Counting words in a string - c programming

后端 未结 12 2142
有刺的猬
有刺的猬 2020-12-07 01:52

I need to write a function that will count words in a string. For the purpose of this assignment, a \"word\" is defined to be a sequence of non-null, non-whitespace characte

12条回答
  •  遥遥无期
    2020-12-07 02:38

    #include
    
    int main()   
    {    
    char str[50];    
    int i, count=1;  
    printf("Enter a string:\n");    
    gets(str);    
    for (i=0; str[i]!='\0'; i++)   
            {
            if(str[i]==' ')    
                    {
                    count++;
                    }
            }
    printf("%i\n",count);    
    }
    

提交回复
热议问题