C++ help, homework Question

Jan 25, 2008 at 6:33pm
How do I write a C++ program that takes a string containg a full name, and outputs each part of the name seperately?

const string fullname= "Horatio Thadeous Hornblow"

using only simple codes like length, size, adding, and subtracting.
Jan 25, 2008 at 7:06pm
I can't use loop or i will get a deduction

What i use is ;

const string fullname= "Horatio Thadeous Hornblower";

string firstname;

string middlename;

string lastname;

int spacePos= fullname.find(' ');

firstname = fullname.substr(0, spacePos);

middlename = fullname.substr(spacePos + 1, .........

and that is where i got stuck, getting the middle name by itself
Jan 25, 2008 at 9:59pm
For something this simple, you could try using rfind(' ') which does almost exactly the same thing as find(' ') _except_ it starts at the end of the string. So;

1
2
3
4
5
6
7
    int spacePos1 = fullname.find(' ');        // gets position of FIRST space     
    int spacePos2 = fullname.rfind(' ');       // gets position of LAST space
    int middlelength = spacePos2 - spacePos1;  // calculates length of name
    // NOW output "middlelength" chars starting at position "spacePos1+1"
    // "spacePos +1" and "middlelength-1" because we DON'T want to include
    // spaces: i.e. we want "middlename" NOT " middlename "
    middlename = fullname.substr(spacePos1 + 1, middlelength - 1); 
Last edited on Jan 25, 2008 at 10:36pm
Jan 25, 2008 at 10:41pm
ok that is great with the int spacPos2 = fullname.rfind(' ');......// but i cant use .rfind to find it again or I will get a deductions. how about a way; of fullname length - (firstname + spacepos1) as a new variable and then search for the spacepos2 there?
Jan 25, 2008 at 10:56pm
how about

int spacepos = fullname.find(' ');

string fn = fullname.subtr(spacepos, 100);

int spacepos2 = fn.find(' ');

firstname= fullname.subtr(0, spacepos);

middlename= fn.substr(0 , spacepos2);

lastname= fn.subst(spacepos2 + 1, 100);
Last edited on Jan 26, 2008 at 12:11am
Jan 25, 2008 at 11:58pm
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.
Last edited on Jan 26, 2008 at 12:08am
Jan 26, 2008 at 12:07am
well, my class is a beginner class and the teacher said we cannot use code we have not covered and only use the ones we were taught. And this is the first time i saw .rfind function, but i will ask my prof if it is allowed.

Thank you for your advice
Last edited on Jan 26, 2008 at 12:07am
Jan 26, 2008 at 12:10am
I wouldn't ask -- he'll probably figure out that you've been looking for help on forums :) and might think you simply stole somebody else's answer -- which you didn't, you worked it out on your own with a little help.
Last edited on Jan 26, 2008 at 12:11am
Jan 26, 2008 at 12:17am
Ok, I won't ask the teacher. Thanks for your help. Learned a lot of tips.
Jan 27, 2008 at 4:22pm
Most teachers support the idea of using the internet as a valid resource--as in the "real world", you can use it to look up functions, syntax, get help, etc. As long as you are able to explain the code, I would think that the instructor would not have a problem with it (although I guess that every instructor is different).
Topic archived. No new replies allowed.