Text Operators

10.07.2017 |

Episode #7 of the course Coding сoncepts with Scratch by Alan Cohen

 

This lesson introduces you to text operators. They allow you to work with and manipulate text. For example, many websites require that your password meets a minimum number of characters. Your password must also contain special characters ($, #, %). Text operators allow you to check if these requirements are met, in addition to performing other functionality.

 

Length of Operator

Let’s write a program that asks the user to create a password that must be between eight and 15 characters. The user enters their password, and the program stores it in the variable Answer.

The program checks the length of the password. The Length Of operator determines the length of the password in the Answer variable.

If the password is either fewer than eight characters or more than 15 characters, the program displays a message that your password must be between eight and 15 characters.

If the password meets the length requirement, the program displays a message that your password was created successfully.

In our next example, our program checks to see if the user’s password contains either the dollar sign ($) or number sign (#). To do this, we use the Letter Of operator.

The program uses a variable named x. This variable is initially set to 1. It references each letter of our password—for example, letter 1 of the password, letter 2 of the password, and so on.

We use a Repeat block to check each letter of the password. The maximum number of times the Repeat block executes is the length of the password that you entered.

The program uses an IF-Then-Else block to determine if the password meets the requirements. This block checks to see if the current character is either a $ or a #. If it is, the program displays a message that the password was successfully created and the program ends.

If not, the variable x increments by one to check the next character of the password. An additional If-Then block checks to see if the current character is the last character of the password. If it is, the password does not contain either a $ or #, and a message displays, telling the user that the password does not meet the requirements.

Join Operator

The Join operator combines two words. This is useful when you need to combine information that the user enters. For example, a program asks a user to enter their first name and last name. You want your program to display both names as one name.

The program begins by asking the user to enter their first name. The first name variable stores the answer. The program now asks the user to enter their last name. The last name variable stores the answer. The program uses the Join operator to combine and display the first and last name.

Here is the output.

Notice anything wrong? There is no space between the first and last name. The Join operator did exactly what you told it to do: join the first and last name. Programs do what you tell them to do, not what you want them to do. You need to add a space between the first and last name. Change the first ask block to instruct the user to do so.

Here is the complete program.

Here is the output.

In our next lesson, you’ll learn how to create lists.

 

Recommended book

Super Scratch Programming Adventure!: Learn to Program by Making Cool Games by The LEAD Project

 

Share with friends