Are the characters in some ASCII ranges or are they arbitrary?
If they are arbitrary, you can create a boolean array:
Here's the algorithm for avoiding a gazillion comparisons:
1. Use bzero() or memset() to zero out isValidChar[] first: faster than looping through 256 characters.
2. Mark the ones you want valid to be true. For example, if you want 'a' to be valid do this:
Of course, if there are more valid characters than invalid ones, you may want to memset() all of them to true, and set the invalid ones to be false. It's up to you. Of course, the compact way of doing this is to create a string:
|
const char *myValidChars = "adfhklmpqsuvz";
|
and loop through the string to initialize isValidChar[]. Now you are ready to do your checks.
3. Loop through your string, character by character, indexing into isValidChar[] to see if each character is valid.
Now, you only have to do one comparison, per character, instead of up to 'n' comparisons where 'n' is the number of valid characters. Try writing the code yourself and if you still have questions, post.
If they are not arbitrary, you should look into convenience functions like isalpha() or isnumeric().