当前位置:   article > 正文

坐牢第三十天(c++)

坐牢第三十天(c++)

1.作业:

 提示并输入一个字符串,统计该字符串中字母个数、数字个数、空格个数、其他字符的个数

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string>
  4. using namespace std;
  5. int main(int argc, char const *argv[])
  6. {
  7. string str;
  8. cout << "请输入一个字符串:";
  9. getline(cin,str);
  10. int len = str.length();//字符串实际长度
  11. cout << "字符串的长度为:";
  12. cout << len << endl;
  13. int alphabet=0;//字母个数
  14. int number=0;//数字个数
  15. int space=0;//空格个数
  16. int other=0;//其他字符个数
  17. for (int i = 0; i < len; i++)
  18. {
  19. if (str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z')
  20. alphabet++;
  21. else if(str[i]>='0'&&str[i]<='9')
  22. number++;
  23. else if(str[i]==' ')
  24. space++;
  25. else
  26. other++;
  27. }
  28. cout << "字母有:"<< alphabet <<endl;
  29. cout << "数字有:"<< number <<endl;
  30. cout << "空格有:"<< space <<endl;
  31. cout << "其他字符有:"<< other <<endl;
  32. return 0;
  33. }

效果图:

 

2.思维导图:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/1022442
推荐阅读
相关标签
  

闽ICP备14008679号