Printing names from strings

So I am trying to write a program that reads in a first middle and last name and then returns in the name in the form: Last, First MiddleInitial. (with the period) for example: Input: Sarah Anne Jane would be Jane, Sarah A.
The program also needs to account for those inputs that do not give a middle name. I am completely lost as to how to configure my program to detect if there is a middle name or not. Also, I am just reprinting the name whereas I need to be printing the last name first. Any help would be great!!!!


#include <iostream>
#include <string>

using namespace std;
string nextString(string list, int index);
int split(string names, string array[], int n);

int main()
{
char ans;
int value = 30, count;
string first, last, name, full, ray[value];

do
{
//Obtain the full name

cout << "Please Enter Your First Middle and Last name" << endl;
getline (cin, full);

//Send the string to the splitting function
count = split(full, ray, value);

// Print the new ray
for(int i=0; i<count; i++)
cout << ray[i] << endl;

//Complete the do while loop
cout << "Would you like to continue?" << endl;
cin >> ans;

} while ((ans == 'y') || (ans == 'Y'));

return 0;
}
//Function to find the next index where a new word starts
string nextString(string list, int index)
{
return list.substr(index,list.find('\0',index)-index);
}

//Function to split the names inside the string
int split(string names, string array[], int n)
{
int i=0, start=0;
do
{
array[i]=nextString(names, start);
i++;
start=names.find('\0', start)+1;
} while (start!=0);

return i;
}
You could count the number of spaces in the string. There will be two spaces if three names are entered. and one if two names are entered.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
	string fullName = "";

	cout << "Please enter your full name: " << flush;
	getline(cin, fullName);

	int numSpace = count(fullName.begin(), fullName.end(), ' ');

	if (numSpace == 1)
		cout << "You did not enter a middle name " << endl;
	else if (numSpace == 2)
		cout << "You did enter a middle name " << endl;

	cin.ignore();
	return 0;
		
}


alternatively you could use a vector and a stringstream
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
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
	string fullName = "";
	vector<string> names;
	string temp = "";
	cout << "Please enter your full name: " << flush;
	getline(cin, fullName);

	stringstream ss(fullName);
	while (ss >> temp)
	{
		names.push_back(temp);
	}

	if (names.size() == 2)
		cout << "You entered two names " << endl;
	else if (names.size() == 3)
		cout << "You entered three names " << endl;


	cin.ignore();
	return 0;
		
}
I understand what you did with the first program with counting spaces, I am just lost as to the printing it out in the of last, first beginning middle initial. Thank you for the help on counting spaces!!
Topic archived. No new replies allowed.