Confused

is “tolower()” a built in function? it must be otherwise the previous video makes 0 sense.

Okay so we use the inbuilt islower() method to determine if all letters are lower case. So we cycle through Letter again and ensure each one islower().

Nah I’m confused. Is “Letter” also some kind of inbuilt function, where have we defined it? we never did.

My guess to get it working is:

bool FBullCowGame::IsLowerCase(FString Word) const
{
    for (auto Letter : Word)
	    if (Letter != islower(Letter))
    {return false;}
    	else { return true; }
}

didn’t work but meh.

for(auto Letter : Word)  {...}

This is a so-called range-based for-loop.

auto Letter

defines a new Variable called ‘Letter’, and ‘auto’ means, that the compiler deduces the variables type from the contents of ‘Word’ automatically.

The loop then proceeds to execute its statements on every Content in Word, or, using the name of the new Variable, execute its content on every ‘Letter’ in ‘Word’.

1 Like

tolower() and islower() are both built-in functions available in some standard header file.

About your function:

for (auto Letter : Word) statement

This will consider that Word is a container type, and loop over each one of the things it contains, executing statement each time. In your case, Word is an FString, as declared in the function parameter list. And FString is just an alias for std::string, which is a built-in container type that contains individual characters. So, that line of code will loop (iterate) over each character contained in Word.

Additionally, the for loop above declares a new variable called Letter with type auto, which means that the compiler tries to automatically deduce the actual type. Since the Word container contains characters, the compiler can deduce that auto must actually be char.

The loop executes statement for each character that is being iterated over. Each time, the value of the iterator variable Letter is first assigned to the next character, and then statement is executed.

Note that statement can be a single statement, or a block of several statements inside {curly braces}. In your example, you did something dangerous, because it is not visually clear what the body of the loop actually is (it is the entire if-else clause). I’ve re-formatted it below for more clarity:

bool FBullCowGame::IsLowerCase(FString Word) const
{
    for (auto Letter : Word)
    {
        if (Letter != islower(Letter))
            return false;
	    else
            return true;
    }

}

Now, there are two problems in your implementation. First, Letter is a character, and you are comparing it to the result of islower(Letter), which is a boolean. You should just check the result of islower(). Second, the return true; part will be executed inside the loop. This means that only the first character will ever be checked, since the function will return something – true or false – after checking the condition.