You'd need the == operator to work differently in order for this approach to work. (You can overload it) It would need to compare the single character to each of the characters in the array. In the interests of both maintainability and re-usability, you should really write a function that will take
(1) the character to check
(2) a null-terminated string that contains characters to check against
Returning true if (1) matches any of the chars in (2) - strtok employs an approach vaguely similar - that is, one of using a null-terminated string to pass what is really an array of otherwise-unrelated characters.
An other way you could do it would be to use a switch statement. Since multiple cases can have the same action, you could just string them all together in this naive manner, which would also actually be pretty fast, despite its awkward appearance.
switch (sSentence[index])
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
break;
default:
}
EDIT: Added approach showing the use of a function.
#include <cstdlib>
#include <cstdio>
#include <cstring>
bool isCharOneOf(char lookForMe, const char *inHere)
{
while (*inHere)
{
if (lookForMe == *inHere)
return true;
inHere++;
}
return false;
}
int main(int argc, char *argv[])
{
const char *searchString = "The quick brown fox jumps over the lazy dog";
const char *vowels = "aeiouAEIOU";
int nChars, curIndex, nVowels=0;
nChars = strlen(searchString);
for (curIndex=0; curIndex<nChars; curIndex++)
{
if (isCharOneOf(searchString[curIndex], vowels) == true)
nVowels++;
}
printf("There are %d vowels in '%s'\n", nVowels, searchString);
return 0;
}