当前位置:网站首页>Count the number of words in the string c language

Count the number of words in the string c language

2022-07-07 08:57:00 Please Sit Down

Calculate formal parameters s The number of words contained in the indicated word string , Return... As a function value .

For the convenience of Statistics , Stipulate that each word is separated by a space .

Columns such as : Shape parameter s The string referred to is :This is a c language program., The return value of the function is 6.

#include <stdio.h>
int fun(char *s){
   int n = 0, flag = 0;
   while (*s != '\0'){
      if (*s != ' ' && flag == 0){
         n++;
         flag = 1;
      }
      if (*s == ' ')
         flag = 0;
      s++;
   }
   return n;
}
int main(){
   char str[81];
   int n;
   printf("Enter a line text:");
   gets(str);
   n = fun(str);
   printf("There are %d words in this text.", n);
}
原网站

版权声明
本文为[Please Sit Down]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130629581472.html