解题思路:
解一:多组输入
解二:使用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语言代码)浏览:892 |
奖学金 (C++代码)浏览:2008 |
【偶数求和】 (C语言代码)浏览:646 |
C语言程序设计教程(第三版)课后习题7.4 (C语言代码)浏览:1246 |
WU-整除问题 (C++代码)浏览:612 |
用筛法求之N内的素数。 (C语言代码)浏览:650 |
C二级辅导-进制转换 (C语言代码)浏览:660 |
前10名 (C语言代码)浏览:727 |
简单的a+b (C语言代码)浏览:512 |
C语言程序设计教程(第三版)课后习题8.3 (C语言代码)浏览:459 |