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.

Recommended Answers

All 12 Replies

>using only simple codes like length, size, adding, and subtracting.
Loop through the string and print non-whitespace characters. When you get to whitespace, skip the whitespace, go to a new line and repeat until you get to the end of the string.

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

Perhaps if you called middlename as middleAndLastName instead.

Then repeat what you know.

>I can't use loop or i will get a deduction
That's stupid. That's really really stupid. Feel free to tell your teacher I said that.

>and that is where i got stuck, getting the middle name by itself
You know where the middle name starts right? And you know how to find the next space, right? So what happens if you subtract the index for the next space from the index of the first letter in the middle name? It gives you the length of the middle name, which can be used as the second argument to substr.

I know where it starts but i dont know how to search for the space again

int spacePos2= fullname.find(' '); //will not find the 2nd space and that is it. how would u suggest to find it?

I know where it starts but i dont know how to search for the space again

int spacePos2= fullname.find(' '); //will not find the 2nd space and that is it. how would u suggest to find it?

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?

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, 100);

I found out a way now without the use of loop and .rfind...

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 " ")


Jerks

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

Why not just reuse find() like:

int spacepos2 = fullname(' ', spacePos+1);
middlename = fullname.substr(spacepos+1,spacepos2);
...

I have code for this for only one name...

#include <fstream> //for fıle ınput and output....
#include <iostream>
#include <string>
using namespace std;

int main()
{
string fullname="This is crazy";
string firstname,lastname,middlename;
int spacepos=fullname.find(' ');
firstname=fullname.substr(0,spacepos);
cout<<"First name is  "<< firstname<<endl;
fullname.erase(0,spacepos+1);
spacepos=fullname.find(' ');
middlename=fullname.substr(0,spacepos);
cout<<"Middle name is  "<<middlename<<endl;
fullname.erase(0,spacepos+1);
spacepos=fullname.find('\0');
lastname=fullname.substr(0,spacepos);
cout<<"Last name is  "<<lastname<<endl;
return 0;


}// end of main

Yeah, but your solution destroys the original data!
Mine uses indexes to start in a new place.
no harn done!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.