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");
}
| |