c++新手一問....點用for loop寫個計space既function



係C program...唔係c++...打錯左

1 Answer

  • p
    Lv 6
    1 decade ago
    Favorite 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);

    return 0;


Still have questions? Get your answers by asking now.