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)
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... } }
The isalpha() function in C++ checks if the given character is an alphabet or not. It is defined in the cctype header file.
Checks if the character value is an alphabetic character ranging from A-Z and a-z.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With