3 回答

TA貢獻1775條經驗 獲得超11個贊
在cctype頭文件中有相當數量的,你可以在字符串中的每個字符使用字符分類功能。對于數字檢查,應為isdigit。
以下程序顯示了如何檢查C或C ++字符串的每個字符(就檢查實際字符而言,該過程幾乎是相同的,唯一真正的區別是如何獲得長度):
#include <iostream>
#include <cstring>
#include <cctype>
int main (void) {
const char *xyzzy = "42x";
std::cout << xyzzy << '\n';
for (int i = 0; i < std::strlen (xyzzy); i++) {
if (! std::isdigit (xyzzy[i])) {
std::cout << xyzzy[i] << " is not numeric.\n";
}
}
std::string plugh ("3141y59");
std::cout << plugh << '\n';
for (int i = 0; i < plugh.length(); i++) {
if (! std::isdigit (plugh[i])) {
std::cout << plugh[i] << " is not numeric.\n";
}
}
return 0;
}
- 3 回答
- 0 關注
- 827 瀏覽
添加回答
舉報