0

I'm able to get the first half of string:

 insert1 = tCreatureOne.substr(0, (tCreatureOne.length) / 2

I don't know how to get the second half of the string

insert2 = tCreatureOne.substr((tCreatureOne.length) / 2), ?????)

Here is my code.

// Insert creature two in to the
//middle of creature one.Science!
// Hamster and Emu make a HamEmuster

std::string PerformScience(std::string tCreatureOne, std::string tCreatureTwo)

{

    std::string insert1;
    std::string insert2;
    std::string insert3;


        // first half : 0 to middle

        insert1 = tCreatureOne.substr(0, (tCreatureOne.length) / 2); 

    // last half: from middle to the end
        insert2 = tCreatureOne.substr((tCreatureOne.length) / 2), tCreatureOne.length); 

        insert3 = insert1 + tCreatureTwo + insert2;

    return insert3;
Peter O.
  • 32,158
  • 14
  • 82
  • 96
  • 1
    You can't without calling `substr()` again! Check [this](http://stackoverflow.com/questions/236129/split-a-string-in-c) out. – fordcars Mar 20 '16 at 03:36
  • 2
    From http://www.cplusplus.com/reference/string/string/substr/ I suspect you're looking for `string::npos`. I.e. `insert2 = tCreatureOne.substr((tCreatureOne.length) / 2), std::string::npos);` – Disillusioned Mar 20 '16 at 03:45
  • 4
    @CraigYoung `std::string::npos` is already the default. It's not necessary to specify the 2nd parameter at all. – πάντα ῥεῖ Mar 20 '16 at 03:53

2 Answers2

0

πάντα ῥεῖ is correct in their comment - to retrieve the second half of your string, you don't need to specify a second parameter (the end of the string):

insert2 = tCreatureOne.substr(tCreatureOne.length() / 2);

The line above will work perfectly fine. Also, since you're using std::string, remember to add the parentheses to the length() call.

0

Probably the most important developer skill is knowing how to do online research. A google search for "c++ substr" reveals this as the top result: http://www.cplusplus.com/reference/string/string/substr/

In the section describing parameters, len is described as follows:

Number of characters to include in the substring (if the string is shorter, as many characters as possible are used).
A value of string::npos indicates all characters until the end of the string.

So you could write:

insert2 = tCreatureOne.substr(tCreatureOne.length() / 2), std::string::npos);

However, note that substr is declared as follows:

string substr (size_t pos = 0, size_t len = npos) const;

Meaning len quite conveniently defaults to npos.
Therefore you could more simply write:

insert2 = tCreatureOne.substr(tCreatureOne.length() / 2));

However, even if substr didn't have such a convenient means of specifying 'the rest of the string', you could still have quite easily calculated it as follows:

int totalLength = tCreatureOne.length();
int firstLength = totalLength / 2;
int remainderLength = totalLength - firstLength;

//So...
insert2 = tCreatureOne.substr(tCreatureOne.length() / 2), remainderLength);
Disillusioned
  • 14,635
  • 3
  • 43
  • 77
  • I made a mistake. I should write tCreatureOne.length() instead of tCreatureOne.length thank you for your help, ur answer really helped me understand everything about substring command and help finished my hw on time – Harrison Tran LamBigini Mar 20 '16 at 09:21