problems with strings

I have to take a name that is in the format of Last, First Middle and give output that takes the string and puts it in the following format:
First Name Middle Initial Last Name with no commas in the final output. If someone could help me figure this out I would greatly appreciate the help. I haven't figured out how to used the find command to make this work properly.


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
/******************************************************************************
 * stringProg.cpp
 * 
 *
 * Program string myString and manipulation of the string.
 ******************************************************************************/
 
 #include <iostream>
 #include <string>
 
using namespace std;

void inputData(string & myString);
void outputData(string myString);


int main()
{
	string myString;
	inputData(myString);
	outputData(myString);
	return 0;
}

void inputData(string & myString)
{
	cout << "Please type your full name in the following format:" << endl;
	cout << "Last, First Middle  -- Note there is no comma between First and Middle" << endl;
	string first, middle, last;
	getline(cin, myString);
	
	int index = myString.find(",", 0);
	last = myString.substr(0, index); //get last name and then erase that part
	myString.erase(0, index + 1);     //of the string up to the index
	
	index = myString.find(" ", 0);
	first = myString.substr(0, index);//get first starting with space after comma
	myString.erase(0, index + 1);     //and erase that plus one
	
	index = myString.find(" ", 0);
	middle = myString.substr(0, index);//use what is left of myString and take
	myString.erase(0, index + 2);      //just the first character
	
	myString += first + " ";
	myString += middle + " ";//myString adding each part of the name together
	myString += last;
	
	myString.insert(first.length() + middle.length() + 1, " " + last.substr(0,1));
	
}

void outputData(string myString)
{
	cout<< myString << endl;
	system("pause");
	

}
Last edited on
Would this be it?

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

void inputData(string & myString);
void outputData(string myString);


int main()
{
	string myString;
	inputData(myString);
	outputData(myString);
	return 0;
}

void inputData(string & myString)
{
	cout << "Please type your full name in the following format:" << endl;
	cout << "Last, First Middle  -- Note there is no comma between First and Middle" << endl;
	string first, middle, last;
	getline(cin, myString);
	
	int index = myString.find(",");
	last = myString.substr(0, index); //get last name and then erase that part
	myString.erase(0, index + 2);     //of the string up to the index
	std::cout << "Last name: " << last << '\n';
        
	index = myString.find(" ");
	first = myString.substr(0, index);//get first starting with space after comma
	myString.erase(0, index + 1);     //and erase that plus one
	std::cout << "First name: " << first << '\n';
        
	index = myString.find(" ");
	middle = myString.substr(0, index);//use what is left of myString and take
	myString.erase(0, index);      //just the first character
	std::cout << "Middle name: " << middle << '\n';
        
	myString += first + " ";
	myString += middle + " ";//myString adding each part of the name together
	myString += last.substr(0, 1) + '.';
	
	//myString.insert(first.length() + middle.length() + 1, " " + last.substr(0,1));
	
}

void outputData(string myString)
{
	cout<< myString << endl;
	system("pause");
	

}
Its a good idea to use find_first_not_of() to eliminate multiple spaces

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	// Remove leading spaces
	myString.erase(0, myString.find_first_not_of(' ', 0));

	// First name
	int	index = myString.find(",", 0);
	last = myString.substr(0, index);
	
	// Gets rid of extra spaces, or if there's just a comma
	myString.erase(0, myString.find_first_not_of(' ', index+1));

	index = myString.find(" ", 0);
	first = myString.substr(0, index);
	myString.erase(0, myString.find_first_not_of(' ', index)); 
	
	// Middle initial
	middle = myString.substr(0, 1);
Thanks guys. I racked my brain on this but finally got it working. Here is the final 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
47
48
49
50
51
52

#include <iostream>
#include <string>

using namespace std;

void inputData(string & myString);
void outputData(string myString);


int main()
{
    string myString;
    inputData(myString);
    outputData(myString);
    return 0;
}

void inputData(string & myString)
{
    cout << "Please type your full name in the following format:" << endl;
    cout << "Last, First Middle  -- Note there is no comma between First and Middle" << endl;
    string first, middle, last;
    getline(cin, myString);    
    
    int index = myString.find(',', 0);
    last = myString.substr(0, index); //get last name and then erase that part
    myString.erase(0, index + 1);    //of the string up to the index
    
    while (myString[0]==' ') myString.erase(0, 1);    //erase left 
 
    index = myString.find(' ' , 0);
    first = myString.substr(0, index);//get first starting with space after comma
    myString.erase(0, index + 1);    //and erase that plus one
    while (myString[0]==' ') myString.erase(0, 1);    //erase left      
    
    middle=myString.substr(0, 1); //get middle initial
    myString.erase(0,myString.length());//make it empty
    myString+=first+" "+middle+" "+last;//create myString as first middle last 
   
    myString.insert(first.length()+2,".");  //insert period after middle initial
    
   //this assignment was a big headache ... glad it finally works
}

void outputData(string myString)
{
    cout<< myString << endl;
    system("pause");
   

}
Good that you got it working.
I think there's an easier way.
1
2
3
4
5
6
7
8
9
10
void inputData(string & myString)
{
  cout << "Please type your full name in the following format:" << endl;
  cout << "Last, First Middle  -- Note there is no comma between First and Middle" << endl;
  string first, middle, last;
  getline(cin, last, ',');
  cin >> first >> middle;

  myString = first + " " + middle[0] + " " + last;
}
Topic archived. No new replies allowed.