//本人复杂了的方法
#include <stdio.h>
#include <ctype.h>
#include <string.h>
char* trimSpace(char *str){
if (str == NULL || *str == '\0'){
return NULL;
}
while (!isalpha(*str) && *str != '\0'){
++str;
}
return str;
}
char* trimWord(char *str){
if (str == NULL || *str == '\0'){
return NULL;
}
while (isalpha(*str) && *str != '\0'){
++str;
}
return str;
}
int main(int argc, char *argv[]){
char buf[200];
memset(buf, '\0', sizeof(buf));
while (fgets(buf, sizeof(buf), stdin) != NULL){
int n = 0;
char *tmp = trimSpace(buf);
while ((tmp = trimWord(tmp)) != NULL){
++n;
tmp = trimSpace(tmp);
}
printf("%d\n", n);
memset(buf, '\0', sizeof(buf));
}
return 0;
}
//简单明了的方法
#include <stdio.h>
int main(){
char ch;
int flag = true;
int count = 0;
while ((ch = getchar()) != EOF){
if ((ch >= 'A' && ch <= 'z') || (ch >= 'a' && ch <= 'z')){
if (flag){
++count;
flag = false;
}
}
else if (ch == '\n'){
printf("%d\n", count);
count = 0;
flag = true;
}
else{
flag = true;
}
}
return 0;
}
UVaOJ 494 - Kindergarten Counting Game
最新推荐文章于 2014-06-02 10:05:05 发布