2. C++ 统计字符串中英文字母、空格、数字和其它字符的个数


输入一行字符,分别统计其中字母、空格、数字和其他字符的个数。
1
// 2 // Created by Green on 2021/6/9. 3 // 4 #include <iostream> 5 6 using namespace std; 7 int letters, digit, space, others; 8 9 int main(void) { 10 cout << "please input some characters" << endl; 11 int c; 12 while ((c = getchar()) != '\n') { 13 if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') { 14 letters++; 15 } else if (c == 32) { 16 space++; 17 } else if (c >= '0' && c <= '9') { 18 digit++; 19 } else { 20 others++; 21 } 22 } 23 cout << "letters: " << letters << endl 24 << "digit: " << digit << endl 25 << "space: " << space << endl 26 << "others: " << others << endl; 27 return 0; 28 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM