I'm supposed to write a program to read a text file and display the following:
a) alphabetic letters //finished with
b) digits // finished with
c) other characters that are not alphabetic letters and not digits //finished with
d) total characters read //need help
The bold part above confused me, by total characters, does it mean the alphabetic letters + other characters? Also, how would I put that in my code?
Code:
#include <stdio.h>
int main (void)
{
int curCh;
int countCh = 0;
int digits = 0;
int countAllOtherCh = 0;
int countTotalCh = 0;
FILE* sp1;
if (!(sp1 = fopen("c:\\bb\\source.txt", "r")))
{
printf("Error opening p07-07.DAT for reading");
return (1);
}
while ((curCh = fgetc(sp1)) != EOF)
{
if (isalpha(curCh))
countCh++;
else if (isdigit(curCh))
digits++;
else
countAllOtherCh++;
}
printf("Number of alphabetic letters is: %d\n", countCh);
printf("Number of digits is: %d\n", digits);
printf("Number of all other characters is: %d\n", countAllOtherCh);
printf("Number of total characters is: %d\n", countTotalCh);
printf("\n\n");
return 0;
} // main