Question

Write a complete C program that inputs a paragraph of text and prints out each unique...

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);

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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;

}

Add a comment
Know the answer?
Add Answer to:
Write a complete C program that inputs a paragraph of text and prints out each unique...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • -I need to write a program in C to store a list of names (the last...

    -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){...

  • T/F C Language Questions. Answer the following true/false questions. You must correctly state WHY your answer...

    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...

    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!

  • How would I change the following C code to implement the following functions: CODE: #include <stdio.h>...

    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:...

  • C programming Question1 (a) Write a C program that will print out all command line arguments,...

    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...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    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...

  • Write a program in C that takes a file name as the only argument on the...

    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 ,...

  • My question is listed the below please any help this assignment ; There is a skeleton...

    My question is listed the below please any help this assignment ; There is a skeleton code:  copy_file_01.c #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { char ch ; FILE *source , *target;    if(argc != 3){ printf ("Usage: copy file1 file2"); exit(EXIT_FAILURE); } source = fopen(argv[1], "r"); if (source == NULL) { printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } target = fopen(argv[2], "w"); if (target == NULL) { fclose(source); printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } while ((ch...

  • Need help in C (a) Write a C program to read in a line of text...

    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...

  • need this in c programming and you can edit the code below. also give me screenshot...

    need this in c programming and you can edit the code below. also give me screenshot of the output #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define LINE_SIZE 1024 void my_error(char *s) { fprintf(stderr, "Error: %s\n", s); perror("errno"); exit(-1); } // This funciton prints lines (in a file) that contain string s. // assume all lines has at most (LINE_SIZE - 2) ASCII characters. // // Functions that may be called in this function: // fopen(), fclose(), fgets(), fputs(),...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT