简体   繁体   中英

how to split the first, middle and last names, and print it as last name, first initial middle initial c++

I have to intput first, middle and last name of a person and the output should be last name, first initial and middle initial if the name has one, but I am having problems trying to take out the middle initial and when I don put a middle name the code prints last name, first initial and last initial any solutions?

 #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
    
       /* Type your code here. */
       string firstName, middleName, lastName, fullName;
       
       getline(cin, fullName);
       
       char firstSpace = fullName.find_first_of(' ');
       char secondSpace = fullName.find_last_of(' ');
       firstName = fullName.substr(0, firstSpace);
       middleName = fullName.substr(fullName.find(' ') + 1 , fullName.find (' ') );
    
       if (secondSpace < 0) {
         lastName = fullName.substr(firstSpace);
         cout << lastName + ", " + firstName.substr(0,1)+ "." << endl;
         
       }
       else{
         lastName = fullName.substr(secondSpace + 1);
         cout << lastName + ", " + firstName.substr(0,1) + "." + middleName.substr(0,1) + "." << endl;
       }
       return 0;
    }

You can try using firstSpace == secondSpace for the if statment.

str.find_fisrt_of and str.find_last_of returns the index of them. So if there is only 1 space in between, it will return true and enter that logic. For the same reason, firstSpace and secondSpace should probably be declared int instead of char .

Also you are getting your middle name wrong. str.substr takes in two numbers, the first one is the starting index of it, and the second one is the length of it. So instead, you should be doing

middleName = fullName.substr(firstSpace + 1 , secondSpace - firstSpace - 1);

Also you could get the first letter of the string by using str.front() , so you are not creating a new string.

Rather than using + to connect different strings, you should use << . Using + , you are actually adding all of them in to a temporary string object. Using << , you are just pushing them into output.

And the majority of your if else will be the same, instead you could have this part written outside of it:

lastName = fullName.substr(secondSpace + 1);
cout << lastName << ", " << firstName.front() << ".";

Then:

if(firstSpace == secondSpace)
    cout << middleName.front() << ".";
cout << endl;

Also, people will mention why using namespace std is a bad practice

So my final code:

#include <iostream>
#include <string>
    
int main() {
    
  std::string firstName, middleName, lastName, fullName;
  std::getline(std::cin, fullName);
       
  int firstSpace = fullName.find_first_of(' ');
  int secondSpace = fullName.find_last_of(' ');

  firstName = fullName.substr(0, firstSpace);
  middleName = fullName.substr(firstSpace + 1 , secondSpace - firstSpace - 1);
  lastName = fullName.substr(secondSpace + 1);

  std::cout << lastName << ", " << firstName.front() << ".";
  if(firstSpace != secondSpace)
  {
    std::cout << middleName.front() << "."; 
  } 
  std::cout << std::endl;
  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM