Checking Characters Part 2 - For loops

Ben was demonstrating a nested for loop in this exercise and was beginning to say it wasn’t really necessary as you could define and initialize Index to 0 and use only one for loop with Comparison iterating through the FString Word. I tried it and it works great. See the code below! I think this is really neat and tidy as opposed to the nested for loops version.

int32 Index{ 0 };
for (int32 Comparison{ Index + 1 }; Comparison < Word.Len(); Comparison++)
{
if (Word[Index] == Word[Comparison])
{
return false;
}
Index++;
}

Definitely better to use least amount of loops as necessary. If you don’t need it, lose it. Let performance get wasted on things not seen :slight_smile:

Your code is incorrect. It only checks that two consecutive letters are different, not that every letter is unique in the word:

IsIsogram("ab") // your code returns true
IsIsogram("abb") // your code returns false
IsIsogram("aba") // your code returns true but should return false

You’re right! Wasn’t on my game when I wrote that. Appreciate the catch!

Privacy & Terms