Public Group

# Getting unique letters from a word

This topic is 3474 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've tried to get the amount of unique letters from a word for a hangman game (the unique letters are used to check if you have won). This is my code (just the part for getting the unique letters): for(loop = 0; loop < wordlength; loop++) { for(looptwo = 0; looptwo < wordlength; looptwo++) { if(gamestring[loop] == gamestring[looptwo]) { uniqueletters--; } } } Loop and looptwo have no purpose beyond giving me a variable to loop. Wordlength is the length of the word. Gamestring is a string that has the current word that is being guessed. Uniqueletters is obviously the amount of unique letters in the word (by the way, uniqueletters is set to the amount of letters in the word before this). These loops work for any words with less than 3 non-unique letters in the word. As soon as it gets to 3 and over, it counts it too many times. How can I fix this?

##### Share on other sites
There is a very big problem here. It's when loop == looptwo. When loop == looptwo, uniqueletters will be decreased, even if gamestring[loop] actually is unique. Rather than decrease uniqueletters, why not increase it (and start it at 0)? And include a test to do nothing when loop == looptwo? I could write the code for you, but I want you to think about it.

Now for your game design: why do you need to know the number of unique letters in a hangman game? Why not just check if userInput == actualWord?

##### Share on other sites
I need to know unique letters because if there's more than one letter of the same kind in a word, it doesn't need to be entered twice. Like, take "momentum", it has 3 "M"s in it, but they only need to be guessed once. Unique letters are needed to be known in order to win. How many times you have guessed a word right will be checked against the number of letters you have to guess, which is unique letters. I use this way because I have another part of my hangman game where someone can enter in something for another person to guess.

##### Share on other sites
Quote:
 Original post by LordimmI've tried to get the amount of unique letters from a word for a hangman game (the unique letters are used to check if you have won).

There's a simpler way.

Just keep a list of the letters in the word, duplicates and all. Each time the user enters a letter, scan the letters and remove all matching instances. When there are no more letters, the user has won. (Of course you keep track of where we are in the process of constructing the gallows and hanging the player.) No need to determine uniqueness, etc.

Also, most implementations don't let a user enter a letter - whether it's in the word or not - twice.

You can not determine the number of unique letters with a simple nested loop. Say you have a word with three occurrences of a letter. The outer loop stops on the first occurrence and the inner loop locates the second and third. Fine. The outer loop then moves on and stops on the second occurrence; the inner loop goes on to find the third and counts it again. Uh oh.

You need to construct a mapping of each letter to the number of times it occurs (which lets you determine the number of uniques in one pass).

##### Share on other sites
Thanks Oluseyi, it's working now. I forgot to do error checking on the input too, thanks for reminding me also =). I figured out that I couldn't do it with a simple nested loop, that's why I came on here to ask.

##### Share on other sites
Since your problem is solved I don't want to add too much confusion, but if this is C++ you can use std::set as a shortcut to collect unique letters from a string.
std::string gamestring("mississippi");std::set<char> letters(gamestring.begin(), gamestring.end());assert(letters.size() == 4);

1. 1
2. 2
Rutin
25
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 22
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631764
• Total Posts
3002210
×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!