0

This is an example of code I've written:

    string name = "";
    string initials = "";


    name = "First Second Third";

    for(int i=0;i<name.length();i++){
        if(name[i]==32){

            cout << name[i+1];
            initials=name[i+1];


        }
    }

    cout << "\nYour initials are " << initials << ".";

My output is:

"ST"
"Your initials are T."

How do I save the 'S' and 'T' into my "initials" string?

I'm asking if this can be done without resorting to pointers, references, or std:: since I'm not familiarized well enough to begin using those concepts.

Akshit
  • 424
  • 4
  • 15
  • https://en.cppreference.com/w/cpp/string/basic_string/push_back – Cruz Jean Sep 29 '19 at 14:26
  • 1
    `name[i]` gives you a reference, so that doesn't fulfill your requirements. In any case, "since I'm not familiarized well enough" is not a reason not to use something. After all, you are asking here about something you don't know either, so learning something doesn't seem wrong. – Ulrich Eckhardt Sep 29 '19 at 14:34
  • I'm not experienced with C++ but your flow can be simpler by splitting text with space char and on the other hand, you'll get an array of split words which you can iterate over that and extract the first character of each word – Masoud Sep 29 '19 at 14:44
  • Don't hard-code values like 32. That could be the wrong value, and it doesn't show readers of this code (including you later in your life) what the value represents. `if (name[i] == ' ')`. – Pete Becker Sep 29 '19 at 15:24

3 Answers3

0

Instead of using

initials = name[i+1];

You can also use append function like this: (use #include <string>)

initials.append(name,i+1,1);

name is the string object to be appended,i+1 is the position of the 1st character of the sub string from name that you want to append to initials and 1 is the length of the sub string. You can have a look at http://www.cplusplus.com/reference/string/string/append/

Akshit
  • 424
  • 4
  • 15
0

You can use something like below.

string name = ""; string initials = "";
name = "First Second Third";
initials = name[0];   //First letter of first word
for(int i=0;i<name.length();i++)
{
    if(name[i] == 32)
    {
        //cout << name[i+1];
        initials += name[i+1];   //Adding First char of Another word
    }
}
cout << "\nYour initials are " << initials << ".";
  • Hey, thanks so much - that works perfectly. I feel pretty bad about not thinking that. –  Sep 29 '19 at 15:17
0

There are a couple of problems in this code. Biggest one is that it's not robust: if the name doesn't start with a space it skips the first word in the name; if someone types two spaces in a row, the "initial" will be a space; and if someone types a space at the end of the input the "initial" will be '\0'. To guard against these problems, the code should check that the next character actually is a letter:

std::string name = "First Second Third";
for (int i = 0; i < name.length(); ++i) {
    if (name[i] == ' ') {
        ++i;
    }
    if (i < name.length() && name[i] != ' ')
        initials += name[i];
}

But I'd go with more powerful tools than that raw search and that hardcoded space:

std::string name = "First Second Third";
std::string white_space = " \r\t\n";
std::string::size_type pos = 0;
for (;;) {
    pos = name.find_first_not_of(white_space, pos); // skip initial whitespace
    if (pos == name.length())
        break;
    initials += name[pos];
    pos = name.find_first_of(white_space, pos);     // skip remainder of name
}
Pete Becker
  • 74,985
  • 8
  • 76
  • 165