Index of the first character of the last name

Hi, im new here, and im struggling with a hw assignment that asks me for:

a. The index of the first character of the last name
b. The first 3 characters of the first name
c. The last 3 characters of the last name

I think I am pretty much done with b and c, but got no luck getting a right. I would appreciate if someone let me know what should I do.

An example would be like:

Enter name: Grace Hopper
Index of the first character of the last name: 6
First 3 characters of the first name: Gra
Last 3 characters of the last name: per

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
#include <iostream>
#include <string>

using namespace std;

int main() {
string firstName;
string lastName;

cout << "Enter Name: ";
cin >> firstName;
getline(cin, lastName);

cout << "Index of the first character of the last name: ";
cout << lastName << endl; // i don't know what to do here.

cout << "First 3 characters of the first name: ";
cout << firstName.at(0) << firstName.at(1) << firstName.at(2)<< endl;

cout << "Last 3 characters of last name: ";
cout << lastName.at(lastName.size()-3) << lastName.at(lastName.size()-2) << lastName.at(lastName.size()-1) << endl;

return 0;
}
  
For the lastname:
the first index is the length of the firstname + 1, if the space counts then add 1
Topic archived. No new replies allowed.