解题思路:
解一:多组输入
解二:使用gets()
注意事项:
参考代码:
#include<stdio.h> #include<string.h> int main() { int letters = 0, digits = 0, spaces = 0, others = 0; char ch; // 多组输入 while(scanf("%c", &ch) != EOF) //EOF表示end of file,其值为-1,输入时另起一行输入ctrl+z表示结束输入 // 还可以写成while(~scanf(""))形式 { if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) letters ++; else if(ch >= '0' && ch <= '9') digits ++; else if(ch == ' ') spaces ++; else others ++; } printf("%d %d %d %d\n", letters, digits, spaces, others - 1); // 最后一个回车键被记录到others中,因此要减去 return 0; }
#include<stdio.h> #include<string.h> int main() { int letters = 0, digits = 0, spaces = 0, others = 0; char ch[10000]; gets(ch); /// 输入一行 for(int i = 0; i < strlen(ch); i++) /// strlen(ch):得到数组ch的长度 { if((ch[i] >= 'a' && ch[i] <= 'z') || (ch[i] >= 'A' && ch[i] <= 'Z')) letters ++; else if(ch[i] >= '0' && ch[i] <= '9') digits ++; else if(ch[i] == ' ') spaces ++; else others ++; } printf("%d %d %d %d\n", letters, digits, spaces, others); return 0; }
0.0分
2 人评分
回文串 (C语言代码)浏览:2842 |
用筛法求之N内的素数。 (C语言代码)浏览:646 |
C语言程序设计教程(第三版)课后习题6.8 (C语言代码)浏览:522 |
最小公倍数 (C语言代码)浏览:1025 |
C语言程序设计教程(第三版)课后习题9.10 (C语言代码)浏览:819 |
图形输出 (C语言代码)浏览:1375 |
找出最长的字符串来 (C语言代码)浏览:1760 |
C语言程序设计教程(第三版)课后习题7.3 (C语言代码)浏览:403 |
母牛的故事 (C语言代码)浏览:561 |
青年歌手大奖赛_评委会打分 (C语言代码)浏览:2138 |