Isogram solution with FindLastChar() member function

hi ,
i did two implemantions of isogram ; (showing only the second implemantation for clarity)

  • one with classic two loops and checking a character against the other characters
  • second has a loop and uses a member function FindLastChar() which return position of the last occurence of a given character in a string. The logic is
    if the position of a character is not equal to last occurence then it is an Isogram

bool UBullCowCartridge::IsIsogram(const FString& Guess)
{

int32 pos{0};
for(int i=0; i<Guess.Len(); ++i) 
{
    Guess.FindLastChar(Guess[i], pos);
    if(pos != i) return true;
}
return false;

}

2 Likes

Looks like you got it all figured out! Great job implementing isogram. Now for clarity, explain a little bit about what pos{0} is doing.

thanks :slight_smile:

here is the explanation for what the int32 pos{0} is ;
the member function FindLastChar() takes 2 parameters ; one for the character to search for and second parameter is an int32& index to save the last position of the character that was searched for.

So the pos is a variable we temporarily save the index no that FindLastChar() returns.

Then we compare that pos with the position of our current character ; if there is another of the same character the saved value of the pos will be different than our current character pos which is variable i inside the for loop.

if the pos variable and current character pos are different than we return true which means it is an Isogram otherwise the function will continue with the for loop. if the loop is completed than the function will return false which means that FString is not an Isogram

Tips for figuring out member function with IDE ;
if you type Guess. , the IDE shows member functions for an FString and if you select any of them and click on them then info about the function is displayed ; such as parameter lists , return values and such. you can also make a right click and select peek or go to the definition of that function in case you need more info. :slight_smile:

Privacy & Terms