C++判斷一個字符串中是否全是數字

#include <iostream>
#include <cstdio>
using namespace std;

bool IsdigitAll(string str)
{
    for (int i = 0; i<str.size(); i++)
    {
        if (!isdigit(str[i]))
        {
            cout << str << " is not all digit" << endl;
            return false;
        }
    }
    return true;
}

int main()
{
    string str = "123456789";
    if (IsdigitAll(str))
    {
        cout << str << " is all digit" << endl;
    }

    str = "123456789abcdef";
    if (IsdigitAll(str))
    {
        cout << str << " is all digit" << endl;
    }

    return 0;
}

輸出:

123456789 is all digit
123456789abcdef is not all digit
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章