Write a complete C program that inputs a paragraph of text and prints out each unique letter found in the text along with the number of times it occurred. A sample run of the program is given below.
You should input your text from a data file specified on the command line. Your output should be formatted and presented exactly like the sample run (i.e. alphabetized with the exact spacings and output labels). The name of your data file along with the text in the file should be echo printed to the screen. Ignore all characters that are not letters (i.e. you should count only the letters in the range a–z and A–Z).
You should only turn in a stand-alone, complete application program that retrieves the name of the data file from the command line. Be sure to subdivide the work of your program into several functions as appropriate. You may not use any global variables (preprocessor constants are OK).
I have a code written below, but can you create a similar one replacing temp, argc, argv, and EOF with something else because we haven't learned how to use those yet. do use fgets to read.
Need help ASAP please!
//code
#include <stdio.h>
#include <string.h>
//this function reads file and calculate frequencies
void read(FILE *infile, int *small_letters, int *capital_letters)
{
char temp;
while (1)
{
temp = fgetc(infile);
if (temp == EOF)
break;
if (temp >= 'a' && temp <= 'z')
{
small_letters[temp - 'a']++;
}
if (temp >= 'A' && temp <= 'Z')
{
capital_letters[temp - 'A']++;
}
}
}
//this function prints frequencies of letters if not 0
void print(int *small_letters, int *capital_letters)
{
printf("Frequency of each character:\n");
for (int i = 0; i < 27; i++)
{
if (small_letters[i] != 0)
printf("%c: %d\n", 'a' + i, small_letters[i]);
if (capital_letters[i] != 0)
printf("%c: %d\n", 'A' + i, capital_letters[i]);
}
}
int main(int argc, char **argv)
{
//no file name passes
if (argc < 2)
{
printf("No inputfile specified!!");
return 0;
}
char filename[100];
strcpy(filename, argv[1]);
printf("Filename: %s\n", filename);
FILE *infile = fopen(filename, "r");
//can't open file
if (infile == NULL)
{
printf("Can't open input file");
return 0;
}
int small_letters[27] = {0}, capital_letters[27] = {0};
read(infile, small_letters, capital_letters);
fclose(infile);
print(small_letters, capital_letters);
}
If you have any doubts, please give me comment...
#include <stdio.h>
#include <string.h>
//this function reads file and calculate frequencies
void read(FILE *infile, int *small_letters, int *capital_letters)
{
char temp[1000];
while(fgets(temp, 100, infile)){
int i=0;
while(temp[i]!='\0'){
if (temp[i] >= 'a' && temp[i] <= 'z')
{
small_letters[temp[i] - 'a']++;
}
else if(temp[i] >= 'A' && temp[i] <= 'Z')
{
capital_letters[temp[i] - 'A']++;
}
i++;
}
}
}
//this function prints frequencies of letters if not 0
void print(int *small_letters, int *capital_letters)
{
printf("Frequency of each character:\n");
for (int i = 0; i < 26; i++)
{
if (small_letters[i] != 0)
printf("%c: %d\n", ('a' + i), small_letters[i]);
if (capital_letters[i] != 0)
printf("%c: %d\n", ('A' + i), capital_letters[i]);
}
}
int main(int argc, char **argv)
{
//no file name passes
if (argc < 2)
{
printf("No inputfile specified!!");
return 0;
}
char filename[100];
strcpy(filename, argv[1]);
printf("Filename: %s\n", filename);
FILE *infile = fopen(filename, "r");
//can't open file
if (infile == NULL)
{
printf("Can't open input file");
return 0;
}
int small_letters[26] = {0}, capital_letters[26] = {0};
read(infile, small_letters, capital_letters);
fclose(infile);
print(small_letters, capital_letters);
return 0;
}
Write a complete C program that inputs a paragraph of text and prints out each unique...
-I need to write a program in C to store a list of names (the last name first) and age in parallel arrays , and then later to sort them into alphabetical order, keeping the age with the correct names. - Could you please fix this program for me! #include <stdio.h> #include <stdlib.h> #include <string.h> void data_sort(char name[100],int age[100],int size){ int i = 0; while (i < size){ int j = i+1; while (j < size){...
Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the command line to be tested for file open.” Append to the program to output to the text log file a new line starting with day time date followed by the message "SUCCESSFUL". Append that message to a file “7Error_Log_File.txt” . ?newline Remember to be using fprintf using stderr using return using exit statements. Test for file existence, test 7NoInputFileResponse.txt file not null (if null...
T/F C Language Questions. Answer the following true/false questions. You must correctly state WHY your answer is true or false in order to receive credit. #include <stdio.h> #include <string.h> int run_through(int num, char **a) { int i; int check=0; for(i=0;i<num;i++) { printf("%s\n", *(a+i)); if(strcmp(*(a+i), "filename")==0) { check=1; } } return check; } char** find_filename(int n, char **b) { int i; int check=0; for(i=0;i<n;i++) { if(strcmp(*b, "filename")==0) { b++; break; } b++; } return b; } int main(int argc, char **argv)...
I am writing a program in C++, which requires me to read an input text file using command line argument. However, I am using xcode on my Macbook to write C++ program, and use terminal instead of command. How do you use int main(int argc, char** argv[]) to read an input file. My professor requires us not to hard code the text file name like .open("example.txt"); Thank you!
The program is done in C. This program opens a file containing binary or text and reads every byte in the file and writes both the ASCII hex value for that byte as well as it’s printable (human-readable) character (characters, digits, symbols) to standard output. The issue I am having is when the file has multiple lines being read. The first line is read and done properly but the other lines do not work correctly. For instance, the text file...
How would I change the following C code to implement the following functions: CODE: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc != 2) printf("Invalid input!\n"); else { FILE * f = fopen (argv[1], "r"); if (f != NULL) { printf("File opened successfully.\n"); char line[256]; while (fgets(line, sizeof(line), f)) { printf("%s", line); } fclose(f); } else { printf("File cannot be opened!"); } } return 0; } QUESTION: Add a function that uses fscanf like this:...
Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1; FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){ numout = fread(&num, sizeof(int), 1, file); c...
C programming Question1 (a) Write a C program that will print out all command line arguments, in reverse order, one per line. Prefix each line with its index. 6 marks] (b) Consider this C code snippet int a- 100 int b- 42; inte p- &a; int q-b; p qi printf ("%d %d\n" ,a,*p); When this code is executed, what numbers will it print? [2 marks] (c) Consider this C program int main(int argc,char argv) char* target- "Apple" char vord[100] printf...
Write a program in C that takes a file name as the only argument on the command line, and prints out the number of 0-bits and 1-bits in the file int main ( int argc , char * argv [] ) { // Check if the user gave an argument , otherwise print " ERROR : no argument " // Check if the file can be read , otherwise print " ERROR : can ’t read " // Otherwise ,...
Need help in C (a) Write a C program to read in a line of text and count the occurrence of each English alphabet. The lowercase version of a letter is considered the same as the uppercase. To make viewing easy, the frequencies should be presented using a bar chart as follows. You can assume that the input contains only spaces, lowercase letters, uppercase letters and the newline character (i.e. the Enter key). Enter a line of text: Letter ZZz...