Sorry, I don't understand the problem you're now trying to solve. That is, I'm not sure what you mean when you say you can't use rfind() "again". I'm suggesting that you use find() and rfind() only once each;
using find(' ') will locate the position of the first space.
using rfind (' ') will locate the position of the second space.
and with that information surely you can use substr() to locate all three names in the string?
Or do you mean that you are not allowed to use rfind() AT ALL in this program?
If you want to avoid using rfind() at all, I think your suggestion above will work quite well -- although you need a couple of slight adjustments;
1 2 3 4 5 6 7 8 9 10 11 12 13
|
int spacepos = fullname.find(' '); // locate first space
firstname = fullname.substr(0, spacepos); // get from start to space
fn = fullname.substr(spacepos+1); // get a shorter string (omit " ")
spacepos = fn.find(' '); // find first space in shorter string
middlename = fn.substr(0, spacepos); // get from start to space
lastname = fn.substr(spacepos+1); // get from space to end (omit " ")
|
Note that I understand what you're trying to do with
|
fn = filename.substr(spacepos, 100);
|
and
|
lastname= fn.subst(spacepos2, 100);
|
but it's bad practice to try to read 100 characters without being sure where the end of the string is.
You should simply use;
|
fn = filename.substr(spacepos+1);
|
and
|
lastname = fn.substr(spacepos+1);
|
which will read from one char after the space to the end of the string anyway.