計算字串中有幾個word

[心得]

1.include <ctype.h> 可以使用 isalpha() 來判斷是不是字母

 

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main()
{
    char input[1000];
    int word,i; 
  
    while(gets(input))
    {
        word=0;
        i=0;
       
        while(input[i])
        {
            if(isalpha(input[i]) && !isalpha(input[i+1]))
                word++;
            i++;
        }
        printf("%d\n",word);       
    }
   
    return 0;
}

arrow
arrow
    全站熱搜

    RingsACM 發表在 痞客邦 留言(0) 人氣()