c++ help

Syntax:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
     string nameAlter(string name)
{
	string Lname;
	string Rname;
	string x;
	string len;
	int CommaLocation;
		CommaLocation = name.find(',');

	Rname = name.substr(name.find(',') + 1, name.length()) + ',' + 
		name.substr(0, name.find(','));

	cout << Rname;

	Lname = name.substr(name.find(0, name.find(','), name.length()));

	cout << Lname;
 



im have trouble sorting the names

You are given a file consisting of students’ names in the following form: lastName, firstName middleName. (Note that a student may not have a middle name.)

program that converts each name to the following form: firstName middleName lastName.
This should do it:

 
std::string standard_format = name.substr(name.find(',') + 2, std::string::npos) + " " + name.substr(0, name.find(','));


Should be pretty self explanatory. That magic + 2 is there so you grab the first character of the first name instead of the comma and space first.

Here it is in action: http://ideone.com/9tcg8g
Last edited on
Topic archived. No new replies allowed.