There are a few excellent ways to reduce your boredom.
First one is to include <ctype.h>, which has all kinds of functions for testing chars: isdigit(), ispunct(), isalpha(), isupper(), islower(), isctrl(), etc.
And all the above, (and more), can be combined with && , etc.
Now for the special chars, one way to deal with them easily, is to put them all into a single char array:
Code:
char special[]={"!@#$%^&*()"}; //put em all in there
#include (string.h> in your program
then
Code:
len=strlen(s);
for(i=0;i<len;i++) {
if(strchr(special, s[i])) {
printf("Yes, it's a special char\n");
else
printf("No, it's not a special char\n");
}
Another way, mentioned by Matticus above, is to use the ASCII values. Easy to use them indirectly.
Say I want only digits from a string:
Code:
int i, len;
char s[]={"abcd12345efghijkl98pqrstuv54321wxyz");
len = strlen(s);
for(i=0;i<len;i++) {
if(s[i] >= '0' && s[i] <= '9')
printf("Yep! %d is a digit. \n", s[i]);
}
When you get into a situation where you are repeating tedious code, it's best to stop, and re-consider. Because the guys who created C were VERY bright, and they didn't want to do a bunch of tedious repetitive code, any more than you do (probably a lot less).
Chances are, they made a function to do what you need. Not always, but very likely.