Easier way?

Please check my function
i can see its more clean
if i wrong please some tell me

bool UBullCowCartridge::IsIsogram(FString Guess) const
{
	char ex = '\0';
	for (size_t i = 0; i < Guess.Len(); i++)
	{
		if (ex == Guess[i]){
			return true;
		}
		else{
			ex = Guess[i];
		}
	}
	return false;
}

That doesn’t work, you are just checking the previous letter with the current, and assign or return.
1: if ex== Guess you return true, so it is an isogram, but should be false, since an Isogram doesn’t have the same letter twice, and you return false if doesn’t find any, but should be true.
2: For example in “comic” word, your program will work like:

ex  Guess[i]
\0 == c , false, so assign c to ex
c == o , false, so assign o to ex
o == m , false, so assign m to ex
m == i , false, so assign i to ex
i == c , false, so assign c to ex

and finishes with return false (that should be true since doesn’t find coincidences).
The problem is, you never check the first c with the last c. That’s why you need 2 for loops, to check ex with all the Guess[i] values, not just the current, before assign the next letter to ex.

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms