using string.find properly

I'm just learning all the string commands and I am super confused on how to use .find. The goal of this program is to take a user input of there name first mid last (uu-cap) and convert to Last, First Mi (with cap first letter).

I just need some guidance on the use of this member function. Thank you.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
using namespace std;

string change(string);

int main()
{
	string Name;	
	char choice;

	//loop to get multiple names if user selects y/Y
	do {
		cout << "Enter a name first mi last \n";
		getline(cin, Name);
		change(Name);

		cout << "You entered: " << Name;
		cout << " It was converted to: " << change(Name) << endl;

		cout << "Do you wish to enter another name? <Y/N> \n";
		cin >> choice;
		cin.ignore();

	} while (toupper(choice) == 'Y');

	return 0;
}

// string function to recieve string and convert to Last, First Mi
string change(string name)
{
	string first, mid, last;	
	 
	first = name.find(" "); // find first 0 - first "space"
	mid = name.find(" ", '.'); // find mid from "space" to "."
	last = name.find(" ", '0'); // find last from "space" to return/null

	//makes the first position in first/mid/last capital
	first[0] = toupper(first[0]);
	mid[0] = toupper(mid[0]);
	last[0] = toupper(last[0]);
	
	string newName = last + ", " + first + " " + mid;
	return newName;
}
find returns a size_t not a string.
1
2
3
const string name = "Hello World";
size_t pos = name.find('W');
cout << "Pos of 'W' " << pos << '\n';
so how would I use find to pull each string out of a random sentence from the user? I see how to find when I'm selecting something (like "W") but if the user can put what ever in, I'm not seeing how to get one word alone and move on to the next.
you need to combine stuff.

ignoring edge cases, find space, substr(0, result). find space from there, substr(previous end, result). etc. you may need to handle leading spaces, multiple spaces, etc.


you can also just let the machine do it for you.
cin >> first >> middle >> last;
will store the names in 3 strings and strips off any white space.

getline gets the whitespace, which you didn't really seem to want.
Last edited on
Yeah, so my original project used cin and assigned each input. It took me 2 mins and worked perfect. The problem was, I caught the fact we need to use .find to get each line from a full string.

So, I thought I was ignoring the edge casing with the original way.
1
2
3
4
5
string change(string name)
{
	string first, mid, last;	
	 
	first = name.find(" "); // find first 0 - first "space" 


now I run substr(0, first) but that leads to nothing. are you essentially using the .find function to get the first string and then substr() for everything thing else.. or is it more like find, substr, find, substr, find substr?

thanks for all of your help. these strings are really getting confusing. I have been pretty solid on everything else.
Example without error checking:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string input;
  cout << "Enter name is the form first middle last: ";
  getline(cin, input);
  size_t start_pos = 0;
  size_t end_pos = input.find(' ');
  string first = input.substr(start_pos, end_pos);
  cout << "First: " << first << '\n';
  
  start_pos = ++end_pos;
  end_pos = input.find(' ', start_pos);
  string middle = input.substr(start_pos, end_pos-start_pos);
  cout << "Middle: " << middle << '\n';

  start_pos = ++end_pos;
  end_pos = input.find(' ', start_pos+1);
  string last = input.substr(start_pos, end_pos);
  cout << "Last: " << last << '\n';


Enter name is the form first middle last: anna maria baide
First: anna
Middle: maria
Last: baide
Thank you for the response. I think I see what needs done now. I believe one of the big problems was, I wasn't making start and end variables from the beginning. thanks again
Topic archived. No new replies allowed.