Need help with "Strings are your friends until they betray you" excersize

I've been trying to solve the following problem but my code crashes for some reason, and I can't seem to find the problem. Any input?

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


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
#include <iostream>
#include <string>
using namespace std;

string replace(char vowel, int nameLength, string str)
{
	for (int i=0; i<nameLength; i++)
	{
		if (str[i] == vowel)
		str[i]= 'z';
	}
}

int main()
{
	string firstName, lastName, fullName;
	cout << "What is your first name, pal?\n";
	cin >> firstName;
	cout << "And your last name?\n";
	cin >> lastName;
	
	fullName = firstName + " " + lastName;
	int nameLength = sizeof(fullName);
	
	fullName = replace('a', nameLength, fullName);
	fullName = replace('e', nameLength, fullName);
	fullName = replace('i', nameLength, fullName);
	fullName = replace('o', nameLength, fullName);
	fullName = replace('u', nameLength, fullName);
	
	
	
	cout << "Hey, " << firstName << " "<< lastName << endl;
	return 0;
}
Line 23, int nameLength = sizeof(fullName);
is incorrect. It finds the size of an object of type std::string.
That is not what you need. Instead you want to know the length of the character string which is managed by the object. Use the member function size(). (length() is an alias of the same thing)
int nameLength = fullName.size();
http://www.cplusplus.com/reference/string/string/size/
Last edited on
You don't need to calculate or keep track of nameLength on your own. One of the nice things about std::strings is that they keep up with their own length.

Your replace function also says it returns a value, but there's no return statement. Here's your code with the nameLength stuff removed and the return value specified:
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
#include <iostream>
#include <string>
using namespace std;

string replace(char vowel, string str)
{
    for (int i=0; i<str.length(); i++)
    {
        if (str[i] == vowel)
            str[i]= 'z';
    }
    return str;
}

int main()
{
    string firstName, lastName, fullName;
    cout << "What is your first name, pal?\n";
    cin >> firstName;
    cout << "And your last name?\n";
    cin >> lastName;

    fullName = firstName + " " + lastName;

    fullName = replace('a', fullName);
    fullName = replace('e', fullName);
    fullName = replace('i', fullName);
    fullName = replace('o', fullName);
    fullName = replace('u', fullName);


    cout << "Hey, " << firstName << " "<< lastName << endl;
    return 0;
}

booradley60 is absolutely right. I missed that there was no return statement in the replace() function.

Though as an alternative, the string could be passed by reference, allowing it to be modified directly, avoiding the need for either the return or the assignment of the result.
1
2
3
4
5
6
void replace(char vowel, string & str)
{
    for (char & ch : str)
        if (ch  == vowel)
            ch = 'z';
}
Thanks! The program doesn't crash anymore when I try to run it, but it still doesn't replace the vowels with a "z".
It does, but it is doing it in a string called "fullName". You're not printing that, though. You're printing "firstName" and "lastName", which you got from the user.
Pass the string by reference, then the change will be reflected in main

Or return the string that was modified
Also, its worth looking at the example code given for the string function find_first_of() which happens to perform exactly this task in a single loop, removing the need for five separate function calls.

instead of this:
1
2
3
4
5
    fullName = replace('a', fullName);
    fullName = replace('e', fullName);
    fullName = replace('i', fullName);
    fullName = replace('o', fullName);
    fullName = replace('u', fullName);

the code could be simply
 
    replace_vowels(fullName);


http://www.cplusplus.com/reference/string/string/find_first_of/
Topic archived. No new replies allowed.