Simple string manipulation

I'm trying to combine first name with last name, then output the full name, and then output full name in reverse. Also I will be replacing all vowels with z after I figure this out.

The space in between fname and lname in the name variable is leaving the fname behind. Why?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
	string fname;
	string lname;
	string name;
	cout << "Enter your first name: ";
	cin >> fname;
	cout << "Enter you last name: ";
	cin >> lname;
	name = fname + " " + lname;
	stringstream ss(name);
	string temp;
	for (int i=0; i < name.size(); i++){
		ss >> temp;
	}
	cout << "Name combined: " << temp << "\n";

	cout << "Name reversed: ";
	for(string::reverse_iterator rit = temp.rbegin(); rit != temp.rend(); ++rit){
		cout << *rit;
	}
	cout << "\n";
}


I tried the std::noskipws operator but then temp is just blank?
Last edited on
Not sure why you are using a stringstream at all. You already have the full name in the name variable.
I have to replace every vowel in 'name' with 'Z' in a sec. Wouldn't that require something like stringstream?
Last edited on
You could use the stringName.find() methods in a loop.

http://www.cplusplus.com/reference/string/string/find/
Last edited on
Got it! Thanks Lynx!!!!

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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
	string fname;
	string lname;
	string name;
	string vowelArr[6] = { "a", "e", "i", "o", "u", "y" };
	string vowel;
	unsigned found;
	cout << "Enter your first name: ";
	cin >> fname;
	cout << "Enter you last name: ";
	cin >> lname;
	name = fname + " " + lname;
	cout << "Name combined: " << name << "\n";
	cout << "Name reversed: ";
	for(string::reverse_iterator rit = name.rbegin(); rit != name.rend(); ++rit){
		cout << *rit;
	}
	cout << "\nName without vowels: ";
	
	for (int i=0; i < name.length(); i++)
	{
		for (int x=0; x < 6; x++) {
			vowel = vowelArr[x];
			found = name.find(vowel);
		if (found!=string::npos)
			name.replace(found, 1, "z");
		}
	}
	cout << name << endl;
}


ON TO GRAUDATION. I have no experience with classes at all, so I may be at it for a few weeks....
Last edited on
Topic archived. No new replies allowed.