- pLv 61 decade agoFavorite Answer
Here is a program to count the number of spaces in a given text.
Note that if only one statement after the for loop is to be contained in the loop, braces are not required, as in the example.
If the given text resides in a file, it has to be read into a character string.
The source program countspa.c can be displayed in text form.
Here's the program:
#include < stdio.h>
#include < stdlib.h>
#include < string.h> int main(int argc, char *argv)
char text="This is a text to count spaces.";
int i, count=0;
for(i=0;i< strlen(text);i++)if(text[i]==' ')count++;
printf("There are %d spaces in the following text:\n%s\n",count, text);