I found a practice problem on these forums and was trying to write a program for it when I couldn't get it to work correctly.
The problem is as follows:
Strings are your friends, until they betray you.
Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
functions
strings & string functions
Write a program that asks for a user first name and last name separately.
The program must then store the users full name inside a single string and out put it to the string.
i.e.
Input:
John
Smith
Output:
John Smith
★ Modify the program so that it then replaces every a, e, i , o, u w/ the letter z.
i.e.
John Smith -> Jzhn Smzth
So I'm trying to solve the ★ problem and this is my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#include <iostream>
#include <string>
using namespace std;
string replacer(char vowel, int nameSize, string fullName);
int main()
{
string firstName, lastName, fullName;
cout << "Please enter the first name:";
cin >> firstName;
cout << endl;
cout << "Please enter the last name:";
cin >> lastName;
cout << endl;
fullName = firstName + " " + lastName;
cout << "The full name is " << fullName << ".\n";
int nameLength = fullName.size();
fullName = replacer('a', nameLength, fullName);
fullName = replacer('e', nameLength, fullName);
fullName = replacer('i', nameLength, fullName);
fullName = replacer('o', nameLength, fullName);
fullName = replacer('u', nameLength, fullName);
cout << "\nAfter replacement, the full name is " << fullName << ".\n";
return 0;
}
string replacer(char vowel, int nameSize, string fullName)
{
for (int i = 0; i < nameSize; ++i)
{
if (fullName[i] = vowel)
fullName[i] = 'z';
}
return fullName;
}
|
My problem seems to be that no matter what names I input, all the letters ultimately come out as 'z's instead of just the vowels. If I could get any help I would truly appreciate it.
Also, if there is a simpler way to write this code and any of you would be so kind to help, I would appreciate some posts of how this can be done.