3 回答

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個(gè)贊
在cctype頭文件中有相當(dāng)數(shù)量的,你可以在字符串中的每個(gè)字符使用字符分類功能。對(duì)于數(shù)字檢查,應(yīng)為isdigit。
以下程序顯示了如何檢查C或C ++字符串的每個(gè)字符(就檢查實(shí)際字符而言,該過程幾乎是相同的,唯一真正的區(qū)別是如何獲得長(zhǎng)度):
#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 關(guān)注
- 831 瀏覽
添加回答
舉報(bào)