Answer:
#include <iostream> // header file for input output functions
#include <string> // header file for string manipulation
#include <fstream> // for handling file functions
using namespace std; //namespace is used to identify objects like cout, cin
void NamesFunction() {
// Function to convert names to format firstName middleName lastName string Name; //String type variable to store full name
ifstream DataFile; //used to read input from a file
ofstream OutputFile; // used for writing in a file
string lastName; // string type variable to store last name
string firstName; // string type variable to store first name
int position; // used to store position of a specified character in the string
int size; // stores length of full name
DataFile.open("inputtexthere.txt");
//opens text file using object DataFile
OutputFile.open("outputext.txt" );
//opens the text file using object OutputFile
do { //start of Do while loop
getline(DataFile, Name); //reads full names from input stream
position = Name.find(","); // stores position of the , in the name
lastName = Name.substr(0, position);
// stores substring last name from full name
size= Name.length(); //store the length of the entire name
firstName = Name.substr(position + 2, size);
/* stores first name substring from entire name string with the help of specified position */
OutputFile<< firstName << " "; //output first name in text file
cout << firstName << " ";
OutputFile<< lastName << endl; //outputs last name in text file
cout << lastName << endl; }
while (DataFile.eof() == false);
//loop continues until the end of input file is reached
DataFile.close(); //closes input file
OutputFile.close(); } //closes output file
int main(int argc, char *argv[]) //start of main function
{ NamesFunction(); // calls NamesFunction()
}
Explanation:
The brief description of included header files is mentioned in the comments added to the program. Lets begin from the NamesFunction() function.
The string variable Name contains the entire string of full names. Then ifstream class is used which is used to read the input file using an object. The object here is DataFile. ofstream is used to write on a file using an object which is named as OutputFile here. firstName and lastName holds first and last names. position variable stores the specified position. Here the position stores the index position of comma (,) in the string which comes between the lastName and firstName in the input file. size variable holds the length of the Name string.
The function substr is used here to divide the string into substring which is the part of the string that starts at character position and traverses through the specified length of characters.
Here to get the last name in the Name string, substr is used which has two parameters 0 and position means that the substring is obtained starting from 0 position of string and ends where it encounters (,).
Similarly to get the first name, this function is used and it has two parameters; position+2 and size which states that substring will start from two places after (,) and ends till the length of the string. Here position+2 means that it will skip the space between the first name and last name.
Next the names are displayed and stored in the output file via the OutputFile object in the format : firstName middleName lastName
close function will close both the input and output files
Main function calls this function NamesFunction to execute.