-
Notifications
You must be signed in to change notification settings - Fork 0
/
countChars.c
34 lines (29 loc) · 922 Bytes
/
countChars.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*this program asks the user to enter the name of a file.
it will then open the file for reading. Once it has done that
it will count up the number of non-space characters in the file and
display that information*/
#include <stdio.h>
#include <ctype.h>
int main(void){
char fileName[40];
//read from user a file name
printf("enter a file name: ");
scanf("%s",&fileName);
printf("counting characters in %s\n",fileName);
//open the file for reading
FILE* theFile;
theFile = fopen(fileName,"r");
int numChars=0;
char curr;
//count the characters in the file
//read a character from the file
while(fscanf(theFile,"%c",&curr) == 1){
//if it is a non-space count it
if(!isspace(curr)){
numChars++;
}
}
printf("The file has %d non-space characters\n",numChars);
fclose(theFile);
return 0;
}