
在这里,我们将看到如何检查给定的输入是整数字符串还是普通字符串。整数字符串将包含在0-9范围内的所有字符。解决方案非常简单,我们将逐个检查每个字符,然后检查它是否是数字。如果是数字,则指向下一个字符,否则返回false值。
Python v2.4版chm格式的中文手册,内容丰富全面,不但是一本手册,你完全可以把她作为一本Python的入门教程,教你如何使用Python解释器、流程控制、数据结构、模板、输入和输出、错误和异常、类和标准库详解等方面的知识技巧。同时后附的手册可以方便你的查询。
示例
#includeusing namespace std; bool isNumeric(string str) { for (int i = 0; i < str.length(); i++) if (isdigit(str[i]) == false) return false; //when one non numeric value is found, return false return true; } int main() { string str; cout << "Enter a string: "; cin >> str; if (isNumeric(str)) cout << "This is a Number" << endl; else cout << "This is not a number"; }
输出
Enter a string: 5687 This is a Number
输出
Enter a string: 584asS This is not a number










