算一算每行有幾個字(word)。
Word的定義是連續的字元(letter: A~Z a~z)所組成的字。
輸入說明 :
一段文字(string)
輸出說明 :
字數(int)
範例輸入 :
Hello everybody!!
This is school principal speeking.
範例輸出 :
2
5
程式碼:
#include <stdio.h>
#include <string.h>
int main()
{
int i,len,word;
char s1[1000];
while(gets(s1)!=NULL)
{
word = 0;
len = strlen(s1);
for(i=0;i<len;i++)
{
if(('A'<= s1[i] && s1[i] <= 'Z') || ('a'<= s1[i] && s1[i] <= 'z'))
{
word++;
for(;i<len && ('A'<= s1[i] && s1[i] <= 'Z') || ('a'<= s1[i] && s1[i] <= 'z');i++);
}
}
printf("%d\n",word);
}
return 0;
}
http://zerojudge.tw/ShowProblem?problemid=a011
沒有留言:
張貼留言