Strings & Error Msg

Pages: 12
For those of you interested, here's my edited code. If anyone has pointers for how I can make it more efficient, please feel free to let me know! Thanks again for all the help!

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <string>
using namespace std;

string replace_vowels (int, string);
string backwards (int, string);

int main (void)
{
	char goAgain = 'y';
	do
	{
		system ("CLS");
		string first_name;									
		string last_name;
		cout << "Please enter your first name: " << endl;
		cin >> first_name;
		cout << "Please enter your last name: " << endl;
		cin >> last_name;
		string full_name = first_name + ' ' + last_name;		//concatenate strings including a space between the words.		
		cout << "Here's what you entered: " << full_name << endl;
		int string_length = full_name.length();    //call function to determine the string length. 
		string new_string1 = replace_vowels (string_length, full_name);    //call function to replace vowels in string.
		cout << "Here's your name with the vowels replaced: " << new_string1 << endl << endl;
		string backwards_name = backwards(string_length, full_name);    //call function to get the name backwards.
		cout << "And here's your name backwards: " << backwards_name << endl << endl;
		do
		{
			cout << "Would you like to enter another name?  (Y or N)" << endl;
			cin >> goAgain;
			if (goAgain != 'y' && goAgain != 'Y' && goAgain != 'n' && goAgain != 'N')
				cout << "You entered an invalid option." << endl << endl;
		}
		while (goAgain != 'y' && goAgain != 'Y' && goAgain != 'n' && goAgain != 'N');
	}
	while (goAgain == 'Y' || goAgain == 'y');
	system ("BREAK");
	return (0);
}

string replace_vowels (int length, string full_name)
{
	string replaced_letters = full_name;    //gives the two strings the same characters to start
	for (int i = 0; i < length; i++)
	{
		if (full_name[i] == 'a' || full_name[i] == 'e' || full_name[i] == 'i' || full_name[i] == 'o' || full_name[i] == 'u')
			replaced_letters[i] = 'z';    //replaces the vowels in the string with 'z'
		else if (full_name[i] == 'A' || full_name[i] == 'E' || full_name[i] == 'I' || full_name[i] == 'O' || full_name[i] == 'U')
			replaced_letters[i] = 'Z';    //replaces the capital vowels in the string with 'Z'
		else
			replaced_letters[i] = full_name[i];
	}
	return (replaced_letters);    //returns the string with vowels replaced
}

string backwards (int length, string full_name)
{
	string backwards = full_name;    //gives both strings the same characters to start
	for (int j = 0; j < length; j++)
		backwards[length-1-j] = full_name[j];    //assigns the first letter of the original string to the last location in the backwards string, etc.
	return (backwards); 
}
Topic archived. No new replies allowed.
Pages: 12