How to Track a String & Duplicates

I need help to understand putting all entries into first string. Then I need to search the first string and if there are duplicated letters, I put the duplicated letters into second string.

I have read the tutorials. It should be simple? But I can not code it. Here is my unworking code.
#include <iostream>
#include <string>
using namespace std;

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
int main()
{
	string first = "";
	string second = "";
	string letter = "";

	cout << "Enter a lowercase letter:  ";
	cin >> letter;

	while (letter != "ZZZ")
	{
		first = first + letter; //put all entries in first string

		cout << "string first:  " << first << endl << endl; //******test print*******

		// search first string for duplicated letters
		for (int x = 0; x < first.length(); x++)
		{
			if (first.substr(x, first.length() - 1) == letter)
			{
				second = second + letter; //put duplicated letters in second string
			}
		} //end for

		cout << "string second:  " << second << endl << endl; //*******test print*******
	
	cout << "Enter a lowercase letter:  ";
	cin >> letter;

	} //end while

	system("pause");
	return 0;
}
Last edited on
Please, is there help? I have put the code tages on it. I need help, Please!
first.substr(x, first.length() - 1)
What does the above do, according to you?
What you want is first[x].

And instead of things like first = first + letter; you should use the shorthand first+=letter;
Last edited on
Thank you. I am still trying to understand.

I wanted that line to search for all letters except the one just put into the string, otherwise the string shows every letter is duplicated!!
Topic archived. No new replies allowed.