解题思路:
和 1012: [编程入门]字符串分类统计 一模一样,区别只是本题要写子函数。
1012题链接:https://blog.dotcpp.com/a/84267
注意事项:
maxSize一开始设50小了,设成100通过了。
参考代码:
#include <iostream> #include <cstring> using namespace std; const int maxSize = 100; //字符串最大长度 void printEachType(char *str); int main() { char str[maxSize]; cin.getline(str, maxSize); //读输入 printEachType(str); return 0; } void printEachType(char *str){ int letter = 0; int number = 0; int space = 0; int other = 0; for(int i = 0; i < strlen(str); i++){ if((str[i] > 64 && str[i] < 91) || (str[i] > 96 && str[i] < 123)){ letter++; } else if(str[i] > 47 && str[i] < 58){ number++; } else if(str[i] == 32){ space++; } else{ other++; } } cout << letter << " " << number << " " << space << " " << other << endl; }
0.0分
17 人评分
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:760 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:375 |
简单的a+b (C语言代码)浏览:335 |
C语言程序设计教程(第三版)课后习题7.1 (C语言代码)浏览:1228 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:333 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:513 |
WU-printf基础练习2 (C++代码)浏览:2003 |
【矩阵】 (C++代码)浏览:947 |
printf基础练习2 (C语言代码)浏览:617 |
核桃的数量 (C语言代码)浏览:669 |