I have an assignment in which the user enters a string named "Input" and somewhere along the way I am supposed to find the first vowel (aeiou) and replace it with the letter 'u'. I thought I was doing it correctly but the string returns the same as before the function was run. What am I doing wrong??
void firstVowel(int i, string &Input)
{
cout << "Replace the first vowel with a 'u'. (a vowel is aeiou)" << endl;
for (i = 0; i< Input.length(); i++)
{
if (Input[i] == ('a' || 'e' || 'i' || 'o' || 'u'))
{
Input[i]='u';
break;
}
}
cout << Input << endl;
}
Did you think why the correct code should be like Input[i] == 'a' yet your original one compiles?
Something interesting/important to note :)
'a', 'e', 'i', 'o', 'u' are char(97), char(101), etc.
'a' || 'e' || 'i' || 'o' || 'u' is translated into 97 || 101 || //etc , and as non-zero integers evaluate true, the whole condition evalutes true. So, the if body is always executed.