Help with strings please

Implement an algorithm to determine if a string has all unique characters. The user will enters a
sentence and your job is to determine if there is repeating letter in the sentence.
1
2
3
4
5
6
7
8
9
10
 #include <iostream>
#include <sting>

using namespace std; 

int main()
{
	string char;
	cout << " Please enter a string of characters: "
	getline(cin,char)

I am lost after this. Need help finishing it.
Disclaimer: I'm a beginner. There are probably much better ways to do this.
P.S. You should probably spend a couple of hours trying to understand this. Your homework is probably going to get much harder than this.

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

int main()
{
	std::string phrase;
	bool is_unique = true;
	
	std::cout << "Please enter a string: ";
	std::getline(std::cin, phrase);
	
	for(int compared_position1 = phrase.length() - 1; compared_position1 >= 0; compared_position1--) {

		std::string compared_letter1 = phrase.substr(compared_position1, 1);

		for(int compared_position2 = compared_position1 - 1; compared_position2 >= 0; compared_position2--) {
			std::string compared_letter2 = phrase.substr(compared_position2, 1);
			
			if(compared_letter2 == compared_letter1)
				is_unique = false;
		}
	}

	if(is_unique)
		std::cout << "unique";
	else
		std::cout << "not unique";
	
	return 0;
	
}




Last edited on
Topic archived. No new replies allowed.