Use C Programming to: Move into the directory hard . You will create a program w
ID: 672456 • Letter: U
Question
Use C Programming to:
Move into the directory hard. You will create a program with the source code in the file letterCount.c.
The program will get the name of a file from the command line and count the number of times each letter of the alphabet occurs within that file. This means that the letters 'A' and 'a' are considered the same which means you should convert all character to lowercase.
Assume you are given the file lab4-hard-data1.in with contents
Here is a run of your program with that file:
Here is the file lab4-hard-data2.in with contents
Here is a run of your program with that file:
Hints and Suggestions
Here is a rough outline of what your program should look like
If you look at the ASCII table you will notice that lowercase characters are contiguous to each other. You should be able to convert them from their character representation to their index offset in the array.
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
int main() {
/* a buffer to hold the count of characters
* It is initialized to zero on every element */
int count[256] = { 0 };
/* loop counter */
int k;
/* file handle --- in this case I am parsing this source code */
FILE *fp = fopen("ccount.c", "r");
/* a holder for each character (stored as int) */
int c;
/* for as long as we can get characters... */
while((c=fgetc(fp))) {
/* break if end of file */
if(c == EOF) break;
/* otherwise add one to the count of that particular character */
count[c]+=1;
}
/* now print the results; only if the count is different from
* zero */
for(k=0; k<256; k++) {
if(count[k] > 0) {
printf("char %c: %d times ", k, count[k]);
}
}
/* close the file */
fclose(fp);
/* that's it */
return 0;
}
sample output :
char a: 29 times
char b: 4 times
char c: 36 times
char d: 15 times
char e: 49 times
char f: 25 times
char g: 4 times
char h: 22 times
char i: 36 times
char k: 9 times
char l: 19 times
char m: 5 times
char n: 35 times
char o: 38 times
char p: 9 times
char r: 34 times
char s: 22 times
char t: 49 times
char u: 16 times
char v: 1 times
char w: 4 times
char y: 2 times
char z: 3 times
char {: 5 times
char }: 5 times
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.