strings: at and length methods

/* I am supposed to use the at and length methods. The program needs to accept the a string containing a person's full name in the format of first name space last name. My function must return the first name in one variable and the last name in a separate variable.

I can't figure out how to approach this. Im not sure how the length method is supposed to work. I tried an unrelated sample program and it read the space as well as the letters. My program stops after the first name.*/

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;


string sepname(string name);


int main()
{
string name;
cout<<" Enter name. ";
cin>>name;
sepname(name);

cout<<name<<endl;

system("pause");

return 0;

}


string sepname(string name)
{

for(int i=0; i < name.length(); i++)
{
name=name.at(i)+name.at(i);
}
return name;

}
1. Use pointers or references to return more than one value:
1
2
3
4
5
6
7
8
9
int f(int a,int b,int &c){
	c=a+b;
	return a*b;
}
//...
int a,
	b=f0(3,4,a);
//a==7
//b==12 

2. Parameters passed by value and modified don't change the value of the argument:
1
2
3
4
5
6
7
8
int f1(int a){
	a=a*3;
	return a;
}
//...
int a=8;
f(a);
//a==8 

3. If the return value is not assigned to anything, it is lost.

4. std::string::length() returns the string length of the string:
1
2
3
4
std::string s0="Hello, World!",
	s1="Hello, World!\0Hello, World!";
int l0=s0.length(), //13
	l1=s1.length(); //13 

5. A string can be concatenated to another is the += operator:
1
2
3
std::string s="Hello, ";
s+="World!";
//s=="Hello, World!" 
Last edited on
Another unusual assignment... I don't see any need for the at() or length() methods to separate the first and last names from a string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <sstream>

using namespace std;

void process( const string & name, string & first, string & last )
{
    istringstream sin( name );
    sin >> first;
    sin >> last;
}

int main()
{
    string name, first, last;

    // read in a line of input
    getline( cin, name );
    // split into first and last names
    process( name, first, last );
    // dump names
    cout << first << endl;
    cout << last << endl;

    return 0;
}
I agree it is kind of silly but thats the way the prof wants it.
I'm trying to read the name, one character at a time and then stop
at the space between the first and last name. store that name in
a variable, then read the last name store that in a separate variable
then use the plus operator to combine the two.
This is what I have so far but it's not compiling.
I appreciate the help.





string sepname(string name)
{

string firstname, lastname;
for(int i=0; i < name.at(""); i++)
{
name = name+i;
}
firstname=name;
for(int j=name.at(""); j<name.length()+; j++)
{
name = name+j;
}
lastname=name;
name = firstname+lastname;
return name;

}
So I guess the find method is out, too... Well, here's some help:
1
2
3
4
5
6
7
8
// loop that executes once for each character in a string
for( int i = 0; i < name.length(); ++i )
{
    if( name.at(i) == ' ' )
    {
        // the character at position i in name is a space
    }
}

Topic archived. No new replies allowed.