检查输入是整数还是字符串的C ++程序
由用户提供给定的输入,任务是检查给定的输入是整数还是字符串。
整数可以是介于0-9之间的数字的任意组合,而字符串可以是除0–9之外的任何组合。
示例
Input-: 123 Output-: 123 is an integer Input-: nhooo.com Output-: nhooo.com is a string
下面使用的方法如下-
输入数据。
Applyisdigit()函数,用于检查给定输入是否为数字字符。此函数将单个参数作为整数,并且还返回int类型的值。
打印结果输出。
算法
Start
Step 1->declare function to 检查数字还是字符串
bool check_number(string str)
Loop For int i = 0 and i < str.length() and i++
If (isdigit(str[i]) == false)
return false
End
End
return true
step 2->Int main() set string str = "sunidhi"
IF (check_number(str))
Print " is an integer"
End
Else
Print " is a string"
End
Set string str1 = "1234"
IF (check_number(str1))
Print " is an integer"
End
Else
Print " is a string"
End
Stop示例
#include <iostream>
using namespace std;
//检查数字还是字符串
bool check_number(string str) {
for (int i = 0; i < str.length(); i++)
if (isdigit(str[i]) == false)
return false;
return true;
}
int main() {
string str = "sunidhi";
if (check_number(str))
cout<<str<< " is an integer"<<endl;
else
cout<<str<< " is a string"<<endl;
string str1 = "1234";
if (check_number(str1))
cout<<str1<< " is an integer";
else
cout<<str1<< " is a string";
}输出结果
sunidhi is a string 1234 is an integer
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短