C++之判断字符串是否是数字

文章转载自https://blog.csdn.net/Richard__Ting/article/details/80772174

判断是否为数字
 1 #include <iostream>
 2 #include <iomanip>
 3 #include <string>
 4 #include <cctype> //判断字符类型需要的头文件
 5 using namespace std;
 6 int main()
 7 {   string str;
 8     int len;
 9     int n;
10     int count;
11     cin>>n;
12     for(int i = 0;i < n;i++){
13         cin>>str;
14         count = 0;
15         len = str.length();
16         for(int j = 0;j < len;j++){
17             if(isdigit(str[j])){ //判断字符是否是数字
18                 count++;//计数
19             }
20         }
21         cout<<count<<endl;
22     }
23     return 0;
24 }
其他

cctype中还有其他函数,如:

isalnum() 用来判断一个字符是否为英文字母或数字,相当于 isalpha(c) || isdigit(c)

isalpha() 用来判断一个字符是否是英文字母,相当于 isupper(c)||islower(c)

原文地址:https://www.cnblogs.com/nxopen2018/p/11989576.html