If we have a string like "CPROGRAMMING"
How can we check if this string contains only
1. Capital letters
2. No digits
Thanks
If we have a string like "CPROGRAMMING"
How can we check if this string contains only
1. Capital letters
2. No digits
Thanks
For starters:
isalpha
Are you familiar with looping through elements of an array?
Seeing what you have done so far would be nice.
isupper() would be handy too.
Mainframe assembler programmer by trade. C coder when I can.
That is what i have just coded
The problem is at read line.isupper() and isalpha() take arguments as integer type however like you said we are passing arg like char type. That is why i am confusedCode:#include <stdio.h> #include <stdlib.h> #include <ctype.h> int main ( int argc , char* argv[]) { char* s = "CPROGRAMMING"; int i; boolean flag = true; for(i=0; , i< strlen(s) && flag == true; i++ ) { if( isalpha(s[i]) && isupper(s[i]) ) flag = true; else flag = false; } if (flag) printf("A string contains only uppercase and alphabet letters\n" ); return 0; }
Last edited by thungmail; 11-05-2009 at 03:43 PM.
Just pass the char as an integer. I'm not sure why they use integers for those functions. It's not like they'll except -1 as an argument. But they expect a regular character in the range 0 to 127.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
Not giving away too much you could then also do:
But yes, those functions will take your passed in char data type as well.Code:... while (*str++) { if ((*str >= 'a') && (*str <= 'z')) ...
>I'm not sure why they use integers for those functions.
>It's not like they'll except -1 as an argument.
Well, technically... Those functions accept int to account for EOF, the idea being that any return value from getchar is an acceptable input to any of the character classification functions (ie. the range of unsigned char, plus EOF). And EOF is often implemented as -1.
>But they expect a regular character in the range 0 to 127.
Beware giving out specific ranges, especially when the range of a type is implementation-defined.
My best code is written with the delete key.
@slingerland3g: >='a' and <='z' can help me.
@King Mir : what do you mean pass char as an integer. Could you explain pls
Thanks all
Thungmail's code is an example. s is a char * and s[i] is therefor a char.
Only s should really be declared as a const char pointer, as it points to a literal string which cannot be changed.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
Last edited by MisterIO; 11-05-2009 at 06:10 PM.