简体   繁体   中英

How to get all initials of name stored in an array using 2 for loops?

The user inputs their full name into the console and gets stored in an array using getline(). The array that the name is stored in is of type "string"

The problem i am trying to solve is as follows:

Include a for-loop that counts the number of names in the full name and then uses that information in a second for loop that displays the full name formally.

So if the user enters in 'Paul Matthew Jones', I want the console to display 'PM Jones'.

There are a few restrictions for the question. I can't use any functions. I must use only 2 loops and I must only use 1 array.

    for (int i = 0; i < myName.length(); i++)
{
    if (myName[i] == ' ')
    {
        nameLength = nameLength + 1;
    }
}

Split the input to individual strings in first place (eg using std::istringstream and a std::vector<std::string> to collect them).
Then just collect all of the first characters and concatenate these, besides for the last string in the split up strings collection which should be appended in whole, and prefixed with a ' ' character.

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