Write a C program that reads input as a stream of characters until encountering
ID: 3585995 • Letter: W
Question
Write a C program that reads input as a stream of characters until encountering EOF (hint: use redirection). Have the program report the number of uppercase characters, the number of lowercase characters, and the number of other characters read. (You can use the functions in ctype.h for this assignment.) Use the attached file infile.txt as input
**Note** Here is the link for the infile.txt file to be used in the program, make sure to use it to test the program, thank you : https://drive.google.com/file/d/0Bx03TriAUEYJNll3X3E3LURKMnc/view?usp=sharing
Explanation / Answer
Hi,
#include <stdio.h>
#include <ctype.h>
int main()
{
FILE *fp = fopen("infile.txt", "r");
int totalUpperCase =0, totalLowercase =0, totalOther =0;
int ch = getc(fp);
while (ch != EOF) {
/* check character is uppercase */
if(isupper(ch)) {
totalUpperCase = totalUpperCase++;;
} else if(islower(ch)) { // check character is lowercase
totalLowercase = totalLowercase++;
} else {
totalOther = totalOther++;
}
// get new char
ch = getc(fp);
}
if (feof(fp)) {
printf("Total uppercase characters: %d",totalUpperCase);
printf(" Total lowercase characters: %d",totalLowercase);
printf(" Total other characters: %d",totalOther);
}
else
printf(" Something went wrong.");
fclose(fp);
getchar();
return 0;
}
let me know if any change required
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.