Why does this work?

My answer for the challenge in 67- Checking Characters Part 2 was a bit different than the answer provided after the challenge and I want to find out why as I was not sure of my answer even though it appears to work fine.

The solution offered after the challenge seems to disagree with what I thought would happen.

I know there are multiple ways to do things but I have no prior experience and want to know why it works.

Thank you!

bool UBullCowCartridge::IsIsogram(FString Word) const

{

// for(int32 Index = 0, Comparison = Index + 1; Comparison < Word.Len(); Comparison++)

// {

//     for(int32 Comparison = Index + 1; Comparison < Word.Len(); Comparison++)

//     {

//         if (Word[Index] == Word[Comparison])

//         {

//             return false;

//         }

//     }

// }    

for(int32 Index = 0, Comparison = Index + 1; Comparison < Word.Len(); Comparison++)

{

    for(Index = 1; Comparison < Word.Len(); Comparison++)

    {

        if (Word[Index] == Word[Comparison])

        {

            return false;

        }

    }

}

return true;

}

It would seem that with my solution any word other than a correct guess output “There are no repeated letters.”

So- it does not work as I stated in the post above.

If I understand what this function is doing (checking the remaining characters past 1 for a match,)

Why would I end up with the PrintLine from the !ISISogram if statement.

If none of the characters in the rest of the string are the same as the character in 1 wouldnt the game read it as a wrong answer and deduct a life instead?

Here is the code incase it is asked for

bool UBullCowCartridge::IsIsogram(FString Word) const

{

// for (int32 Index = 0, Comparison = Index + 1; Comparison < Word.Len(); Comparison++)

// {

//     for(int32 Comparison = Index + 1; Comparison < Word.Len(); Comparison++)

//     {

//         if (Word[Index] == Word[Comparison])

//         {

//             return false;

//         }

//     }

// }    

for (int32 Index = 0, Comparison = Index + 1; Comparison < Word.Len(); Comparison++)

{

    for(Index = 1; Comparison < Word.Len(); Comparison++)

    {

        if (Word[Index] == Word[Comparison])

        {

            return false;

        }

    }

}

return true;

}

Could you describe what your loop does line by line each iteration for the word “plan”?

1 Like

I am sorry but I do not understand your question. I have not used the word plan.

I’m asking for you to state what is happening in your loop if the word “plan” is passed into it. It doesn’t matter what word, I just want you to explain what your loop is doing so you understand why it’s wrong.

What happens line by line?

I understand now. Thank you.

This is what I think:

for (int32 Index = 0, Comparison = Index + 1; Comparison < Word.Len(); Comparison++)

loop (Index is the first character in the string, Comparison is the next character; comparison will not go past the last character in the string; Comparison will look at the next character each loop)

for(Index = 1; Comparison < Word.Len(); Comparison++)

Loop (Index starts at the second character; Comparison still wont go past the last character; Comparison will look at the next character each loop)

    if (Word[Index] == Word[Comparison])

if (the character in the word is the same as the character in the comparison return the Boolean as false

I forgot to add “each iteration” to the last reply. For example with the following code

for (int32 Index = 0; Index < 3; ++Index)
{
    PrintLine(TEXT("%i"), Index);
}

I’m asking you to do something like this

  • Initialise Index to 0
  • Check if Index is < 3, it is so it enters the loop
  • PrintLine with the value of Index (0)
  • Increment Index by 1
  • Is Index (1) < 3, it is so the loop continues
  • PrintLine with … etc.

But for your own code.

1 Like

Ok.
for (int32 Index = 0, Comparison = Index + 1; Comparison < Word.Len();Comparison++)

  • initialize index to 0

  • initialize comparison to index plus 1

  • check comparison is less than string length

  • do again until comparison reaches the end of the string

    for(Index = 1; Comparison < Word.Len(); Comparison++)

  • initialize index to 1

  • check comparison is less than the string length

  • do again until comparison reaches the end of the string

  •     if (Word[Index] == Word[Comparison])
    

if the index value and the comparison value are the same character return false

        return false;

During the challenge my initial thought was I would need to duplicate the code with the index value counting up until it reached the end of the string.

The solution you gave using the Comparison = Index + 1 makes since as with one line of code the loop does that. If I am understanding correctly.

It’s unclear by what you mean but that doesn’t happen all on the first iteration.

technically an assignment not initialisation.


What your code does with the word “tan”

  • Initialise Index to 0, Comparison to 1.
  • If Comparison (1) is < 3 enter loop body
  • Assign Index to 1
  • is Comparison (1) is < 3 enter loop body
  • Is Word[1] == Word[1] the same character, yes so return false.

Which is incorrect as “tan” is an isogram and also will always return false no matter what you enter.

If you remove Index = 1 from the second loop it will still be incorrect because it does the following. Changing the word to “abb” to illustrate the issue with it now

  • Initialise Index to 0, Comparison to 1.
  • If Comparison (1) is < 3 enter loop body
  • is Comparison (1) is < 3 enter inner loop
  • Is Word[0] == Word[1] the same character (a == b), no
  • Increment Comparison by 1 (2)
  • Is Comparison < 3, enter loop body
  • is Word[0] == [2] (t == n), no
  • Increment Comparison (3)
  • Is Comparison < 3, no exit inner loop
  • Is Comparison < 3, no exit loop and return true.

Which is incorrect as “abb” is not an isogram.


What the course does with the word “abb”

  • Initialise Index to 0
  • if Index < 3, enter loop body
  • Initialise Comparison to Index + 1 (1)
  • If Comparison < 3, enter loop body
  • Word[0] == Word[1] (a == b)
  • Increment Comparison (2)
  • If Comparison < 3, enter loop body
  • Word[0] == Word[2] (a == b)
  • if Comparison < 3, false so the loop ends

Now here’s the crucial part

  • Comparison goes out of scope
  • Index gets incremented by 1
  • If Index < 3, enter loop body
  • Initialise Comparison to Index + 1 (2)
  • 2 < 3
  • Word[1] == Word[2] (b == b) so return false.
1 Like

Thank you for your patience, I see it see it now.

1 Like

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

Privacy & Terms