Isogram check ignoring the last letter?

So I’m working through the lecture, getting a little bit stuck without the hints, but then I thought I had it with this

bool UBullCowCartridge::IsIsogram(FString Word) const // Isogram Check
{

    int32 LetterA = 0;
    int32 LetterB = LetterA + 1;

    for (; LetterA < Word.Len(); LetterA++)
    {
        for (; LetterB < Word.Len(); LetterB++)
        {
            if (Word[LetterA] == Word[LetterB])
            {
                return false;
            }
        }
    }  
    return true;
}

I tested it but for some reason whenever a word like “staff” is entered, it doesn’t see the repeating letters, can anyone help me figure out what I’ve missed? After comparing it to the example code, I can’t see why it’s not working.

EDIT: After a little more testing it seems to only be checking the first three letters maybe? It is seeming very inconsistent but I can’t seem to get it to work as it should. Example image attached.

Ok so I rewrote the code EXACTLY as the example suggests, initialising the variables within the for loop like so

bool UBullCowCartridge::IsIsogram(FString Word) const // Isogram Check
{
    // int32 LetterA = 0;
    // int32 LetterB = LetterA + 1;

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

    return true;
}

And that all works fine now.

But why didn’t the other version work? What am I missing?

Write out what your loop does for the word “abb”.

As a demonstration for what I’m asking here’s an example with this loop

int sum = 0;
for (int i = 1; i =< 3; ++i)
{
    sum += i;
}
  • Initialises sum to 0
  • Initialises i to 1
  • Is i (1) <= 3
  • yes so enter loop
  • sum += 1
  • increment i
  • is i (2) <= 3
  • sum += 2
  • increment i
  • is i (3) <= 3
  • sum += 3
  • increment i
  • is i (4) <= 3
  • END
   int32 LetterA = 0;
    int32 LetterB = LetterA + 1;

    for (; LetterA < Word.Len(); LetterA++)
    {
        for (; LetterB < Word.Len(); LetterB++)
        {
            if (Word[LetterA] == Word[LetterB])
            {
                return false;
            }
        }
    }  
    return true;

initialise a to 0 (0)
initialise b to a+1 (1)

is a (0) less than WordLen (3)
yes so enter loop
is b (1) less than WordLen (3)
yes so enter loop
if WordLetter(a = 0) == WordLetter(b = 1)
return did not pass check
b + 1 (2)
is b (2) less than WordLen (3)
yes so enter loop
if WordLetter(a = 0) == WordLetter(b = 2)
return did not pass check
b + 1 (3)
is b (3) less than WordLen (3)
no exit loop
a + 1

is a (1) less than WordLen (3)
yes so enter loop
is b (1) less than WordLen (3)
yes so enter loop
if WordLetter(a = 1) == WordLetter(b = 1)
return did not pass check
b + 1 (2)
is b (2) less than WordLen (3)
yes so enter loop
if WordLetter(a = 1) == WordLetter(b = 2)
return did not pass check
b + 1 (3)
is b (3) less than WordLen (3)
no exit loop
a + 1

is a (2) less than WordLen (3)
yes so enter loop
is b (1) less than WordLen (3)
yes so enter loop
if WordLetter(a = 2) == WordLetter(b = 1)
return did not pass check
b + 1 (2)
is b (2) less than WordLen (3)
yes so enter loop
if WordLetter(a = 2) == WordLetter(b = 2)
return did not pass check
b + 1 (3)
is b (3) less than WordLen (3)
no exit loop
a + 1

is a (3) less than WordLen (3)
no exit loop

return passed

Have I even understood my own logic correctly? xD

Why is b 1 now on the second loop?

It wouldn’t be? Its retains its value from the last loop because no where inside the loop as it is running is the value reset?

Which is why the other code works, because the values are reassigned every time the loop resets?

The second loop would go like this

is a (1) less than WordLen (3)
yes so enter loop
is b (3) less than WordLen (3)
no exit loop
a + 1

Yes, precisely. Another thing is that your b = a + 1 is just a longer way of saying b = 1. Whereas in the courses code on the second loop b = a + 1 would be initialising b to 2 as a is now 1.

Aamzing! Thank you so much for helping develop my understanding on this.

1 Like

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

Privacy & Terms