This code was working for me 30 minutes ago, but all of a sudden it started to return 0's and 1's. It's supposed to count how many vowels does the string I input have.
You're not checking each letter of the string against all your possible vowels.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int countVowels(char * strPtr)
{
int count=0; //running total of vowels found
char vowels[5] = {'a','e','i','o','u'}; //list of all our vowels
while(*strPtr != '\0') //while we haven't reached the end of the string...
{
for(int i = 0; i < 5; ++i) //loop over our list of vowels
{
if(*strPtr == vowels[i]) //if the current character in the string equals a vowel...
count++; //increment our vowel count by one
}
strPtr++; //now that we've looked and compared to each vowel, move to the next char in the string
}
//end of while loop, we've reached the end of the string
return count; //return how many vowels we counted
}