当前位置:   article > 正文

C++ 基础学习

C++ 基础学习

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

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. cout<<"请输入字符串:";
  6. string str;
  7. getline(cin,str);
  8. int num=0;
  9. int alp=0;
  10. int spa=0;
  11. int other=0;
  12. int len=str.length();
  13. for(int i=0;i<len;i++)
  14. {
  15. if(str[i]>='0'&&str[i]<='9')
  16. {
  17. num++;
  18. }else if((str[i]>='a' && str[i]<='z')||(str[i]>='A' && str[i]<='Z'))
  19. {
  20. alp++;
  21. }else if(str[i]==' ')
  22. {
  23. spa++;
  24. }else
  25. {
  26. other++;
  27. }
  28. }
  29. cout<<"数字"<<num<<"个"<<endl;
  30. cout<<"字母"<<alp<<"个"<<endl;
  31. cout<<"空格"<<spa<<"个"<<endl;
  32. cout<<"其他字符"<<other<<"个"<<endl;
  33. return 0;
  34. }

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

闽ICP备14008679号