4 replies
by mmmova
This solution is in Borland Tubo C++ 3
All you have to do is to first of all open a file pointer that points to your file. Then you have too use an infinite loop a do while or while loop and fetch each character using fgetc()
function and check the character against space, if it is found increment a variable and at the end of loop, print the count.
a sample would be,
#include
void main()
{char current;
int spaceCount=0;
FILE *fp; fp=fopen(c://yourfilepath//yourfilename.txt","r+");
current = fgetc(fp);
while(current!=EOF)
{
current=fgetc(fp);
if(current==' ')++spaceCount;
}
fclose(fp);
printf("The number of spaces: %d",spaceCount);
getch();
}
by joltinjim
This solution has a small problem, it does not check to see if the first character is a space.
I would suggest substituting the following code for the loop:
current = fgetc(fp);
while()
{
if(current==' ')++spaceCount;
else if(current==EOF)break;
current=fgetc(fp);
}
by mekijemal
This discussion is closed to new replies.
Trending Web Development posts
Sort by Trending