Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isalpha(<mychar>) == true evaluates to false?

Tags:

c++

string temp is equal to "ZERO:\t.WORD\t1" from my debugger. (the first line of my file)

string temp = RemoveWhiteSpace(data);
int i = 0;
if ( temp.length() > 0 && isalpha(temp[0]) )
    cout << "without true worked" << endl;
if ( temp.length() > 0 && isalpha(temp[0]) == true )
    cout << "with true worked" << endl;

This is my code to check if first character of temp is a a-z,A-Z. The first if statement will evaluate to true and the 2nd to false. WHY?!?!?! I have tried this even without the "temp.length() > 0 &&" and it still evaluates false. It just hates the "== true". The only thing I can think of is that isalpha() returns != 0 and true == 1. Then, you could get isalpha() == 2 != 1. But, I have no idea if C++ is that ... weird.

BTW, I dont need to know that the "== true" is logically pointless. I know.

output was

without true worked

Compiled with CodeBlock using GNU GCC on Ubuntu 9.10 (if this matters any)

like image 765
David Stocking Avatar asked Mar 25 '10 21:03

David Stocking


People also ask

How do you use Isalpha in a for loop?

Isalpha takes as an argument a character, it is easy to use a for loop to iterate over each character in a string: string name = "my name"; // char* name for( int k = 0, s = strlen (name); k < s; k++) { if( isalpha (name[ k ] )) // if( ! salpha(name[k])) If it is not { code... } }

What is Isalpha in CPP?

The isalpha() function in C++ checks if the given character is an alphabet or not. It is defined in the cctype header file.

What is Isalpha in Javascript?

Checks if the character value is an alphabetic character ranging from A-Z and a-z.


2 Answers

The is* functions are only guaranteed to return a non-zero value if true, NOT necessarily a 1. A typical implementation is table based, with one entry in the table for each character value, and a set of bits defining which bit means what. The is* function will just AND the right bitmask with the table value, and return that, which will only be the value 1 for whichever type happens to have been given bit position 0.

E.g.:

#define __digit 1
#define __lower 2
#define __upper 4
extern int __type_table[];

int isdigit(int c) { 
    return __type_table[c+1] & __digit;
}

int isalpha(int c) { 
    return __type_table[c+1] & (__lower | __upper);
}

int islower(int c) { 
    return __type_table[c+1] & __lower;
}

int isupper(int c) { 
    return __type_table[c+1] & __upper;
}

Where __type_table is defined as something like int __type_table[UINT_MAX+1]; and would be initialized so (for example) __type_table['0'+1] == __digit and __type_table['A'+1] == __upper.

In case you care, the '+1' part is to leave a spot at the beginning of the table for EOF (which is typically defined as -1).

like image 181
Jerry Coffin Avatar answered Sep 19 '22 19:09

Jerry Coffin


isalpha doesn't return true, it returns non-zero. This is quite common for API designed for C.

Note that in the expression isalpha(ch) == true, the subexpression true is promoted to type int with value 1.

like image 36
avakar Avatar answered Sep 19 '22 19:09

avakar