Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between isdigit() and isnumber()?

Tags:

c++

string

ctype

I have the function isnumber() in my ctype.h. I don’t find references of this function in the books I have neither in http://www.cplusplus.com/reference/cctype/ for consult. I intend to detect and show the difference between isdigit() and isnumber() by a simple example, but there is not (besides, U+0C6A, U+0ED2 and ½ are undetectable by the functions). Is there any difference? How could I resolve that?

int main(int, char *[])
{
    string text("Hello½123, Poly౪ ໒girl0.5!!!");
    decltype(text.size()) punct = 0, alpha = 0, number = 0, space = 0, digit = 0, graf = 0;
    for(auto i : text) {
        if(ispunct(i)){
            cout << i << "<punct  ";
            ++punct;
        }
        if(isalpha(i)) {
            cout << i << "<alpha  ";
            ++alpha;
        }
        if(isnumber(i)) {
            cout << i << "<number  ";
            ++number;
        }
        if(isdigit(i)) {
            cout << i << "<digit  ";
            ++digit;
        }
        if(isgraph(i)) {
            cout << i << "<graph  ";
            ++graf;
        }
        if(isspace(i))
            ++space;
    }
    cout << endl << "There is " << endl;
    cout << punct <<  " puncts," << endl;
    cout << alpha << " alphas," << endl;
    cout << number << " numbers," << endl;
    cout << digit << " digits," << endl;
    cout << graf << " graphs," << endl;
    cout << space << " spaces" << endl;
    cout << "in " << text << endl;

    return 0;
}

A part of the result:

...

5 numbers,
5 digits,
23 graphs,
2 spaces
in Hello½123, Poly౪ ໒girl0.5!!!
like image 758
Isi Avatar asked Nov 29 '25 03:11

Isi


2 Answers

isnumber() may be an Apple-specific C++ method (I don't have a Mac on hand to check). You can see it in the Apple dev guide:

The isnumber() function behaves similarly to isdigit(), but may recognize additional characters, depending on the current locale setting.


Besides, isnumber() is not declared on Linux: I use g++ 6.1.1 on Linux 4.7.2 and get the error:

g++ a.cpp
a.cpp: In function 'int main(int, char**)':
a.cpp:20:17: error: 'isnumber' was not declared in this scope
   if (isnumber(i)) {
                 ^

I also use clang3.8.1 to test:

clang++ a.cpp --std=c++11
a.cpp:20:7: error: use of undeclared identifier 'isnumber'
                if (isnumber(i)) {
                    ^
like image 110
Q.S Avatar answered Dec 01 '25 16:12

Q.S


isdigit() only works with 0-9.

isnumber() allows other number values such as fractions. Some characters that are 'numeric' but not digits include 0x00b2 and 0x00b3 which are superscripted 2 and 3 ('²' and '³') and the glyphs that are fractions such as '¼', '½', and '¾'.

like image 37
Software_Designer Avatar answered Dec 01 '25 17:12

Software_Designer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!