如何检查C / C ++字符串是否为int?
有几种检查字符串是否为int的方法,其中一种方法是isdigit()用于检查字符串。
这是检查C++语言中的字符串是否为int的示例,
示例
#include<iostream>
#include<string.h>
using namespace std;
int main() {
char str[] = "3257fg";
for (int i = 0; i < strlen(str); i++) {
if(isdigit(str[i]))
cout<<"The string contains int\n";
else
cout<<"The string does not contain int\n";
}
return 0;
}输出结果
这是输出
The string contains int The string contains int The string contains int The string contains int The string does not contain int The string does not contain int
在上述程序中,main()函数中存在检查字符串的实际代码。使用内置方法isdigit(),检查字符串的每个字符。如果字符串字符是数字,它将打印包含int的字符串。如果字符串包含字符或字母,它将打印不包含int的字符串。
for (int i = 0; i < strlen(str); i++) {
if(isdigit(str[i]))
printf("The string contains int\n");
else
printf("The string does not contain int\n");
}热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短