UVaOJ 494 - Kindergarten Counting Game

来源:互联网 发布:c语言break用法if 编辑:程序博客网 时间:2024/06/06 09:13
//本人复杂了的方法#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;}

0 0
原创粉丝点击