My CheckIsIsogram() Implementation

Here’s my take on the function:

[CODE]
bool FBullCowGame::CheckIsIsogram(FString AWord)
{
bool IsIsogram = true;

/* original code... 
for (int32 i = 0; i < (int32)CurrentGuess.length(); i++) {
	int32 CharCount = std::count(CurrentGuess.begin(), CurrentGuess.end(), CurrentGuess[i]);
	if (CharCount > 1) {
		IsIsogram = false;
		break;
	}
}*/

// we need a map to hold the characters
// already seen...
TMap<char, bool> TheLetter;

// if we don't have single-value or null characters...
if (AWord.length() > 1 && AWord != "\0") {
	// loop through the word...
	for (auto AChar : AWord) {
		// for each character in word...
		if (TheLetter[AChar]) {
			// whoops, duplicate letter!
			IsIsogram = false;
			break;
		}
		else
			TheLetter[AChar] = true;
	}
}

return IsIsogram;

}
[/CODE]

I decided on “AWord” as a parameter as that way the function could be used both when setting up a hidden word and also for when checking a user guess.

I also don’t like more than one return in a function if it can be avoided, so just used a flag and returned that flag which is set as appropriate.

Privacy & Terms