Eng:Case Sensing (1.4)

Case sensing is the act of specifying or checking whether text is uppercase or lowercase. It can be done in Scratch using the methods shown.

Checking Case
These methods show how to find out the case of the text or letter you want to check.

Using Lists
This method checks if each letter of the text is uppercase or lowercase. It is reliable and will work online.

Three lists will be needed: A variable called "count" will also be needed.
 * Uppercase
 * Lowercase
 * Results

Add the uppercase letters from A to Z into the list 'Uppercase' and the lowercase letters into the 'Lowercase' list. Leave 'Results' empty.

Add this script into the sprite that is doing the sensing:

when green flag clicked set [count v] to [1] delete (all v) of [Results v] ask [Input a sentence.] and wait repeat (length of (answer)) if <[Uppercase v] contains (letter (count) of (answer))> then add [U] to [Results v] else if <[Lowercase v] contains (letter (count) of (answer))> then add [L] to [Results v]   else add [O] to [Results v]   end end change [count v] by (1) end

The list 'Results' will contain 'U' if the letter is uppercase, 'L' if it is lowercase, and 'O' if it is something else (such as a space or number).

The following example will return the case of a letter from the original sentence (U, L or O). (item (letter #) of [Results v])

Using Costumes
Costume names are case-sensitive and this can be used as a method to determine the case of a letter. For this, a 52-costume sprite must be created with its costumes alphabetically named a to z and A to Z. The following Script is an example of how this costume based-solution can find the case of a letter: set [letter v] to [f] set [case v] to [unknown] switch to costume (letter) if <(costume #) > [26]> then set [case v] to [upper-case] else set [case v] to [lower-case] end The above script will find that the costume named "f" will have a number of 6, which is less than 26 and must therefore be lower-case. Since all of the sprite's first 26 costumes are lower-case letters, the method above knows that if the costume it is wearing after the change has a number greater than 26, it must be in the upper-case half of its costumes. This method basically exploits the costumes tab as a single, case-sensitive list.

If the costumes are set up in the order A, a, B, b, C, c, etc., replace <(costume #) > [26]> with <((costume #) mod (2)) = [1]> This is because every lowercase costume has a modulo value of 0, being a multiple of 2 in the costumes list.

Asking
The user can simply ask what the case is, but the user could lie, or give an answer other than the one coded to accept.

Editing Scratch
Four Boolean blocks can be made by editing the System Browser related to case sensitivity. Three tell whether the inputted text is uppercase, lowercase, or mixed case. The fourth is just like the =  (block) block, except case-sensitive.

First, add the following blockspecs to Operators in  (See more about blockspecs here.)

(With the =,  < , and  >  blocks:)

(In a new section (achieved by typing  outside of a blockspec.) ):

Then, in, add the following codes: case: t1 sensitive: t2 "Answers true if t1 and t2 are equal when converted to strings." ^ t1 asString = t2 asString isLowercase: t1 "Answers true if t1 is not empty and is equal to itself when lowercased." ^ t1 ~= '' and: [t1 asString asLowercase = t1 asString] isUppercase: t1 "Answers true if t1 is not empty and is equal to itself when uppercased." ^ t1 ~= '' and: [t1 asString asUppercase = t1 asString] mixedCase: t1 "Answers true if t1 is not equal to itself when lowercased or uppercased." ^ t1 asString ~= t1 asString asUppercase and: [t1 asString ~= t1 asString asLowercase]

Specifying Case
These methods show how to force text into a certain case.

Using Lists
This method uses the same three lists and variable used in the case sensing method, along with another variable: subcount.

Add these scripts to the sprite:

when flag clicked set [count v] to [0] delete (all v) of [Results v] ask [Input text to be shown uppercase.] and wait repeat (length of (answer)) change [count v] by (1) if <[Lowercase v] contains (letter (count) of (answer))> broadcast [Become uppercase v] and wait else add (letter (count) of (answer)) to [Results v] end end

when I receive [Become uppercase v] set [subcount v] to [0] repeat (26) change [subcount v] by (1) if <(item (subcount) of [Lowercase v]) = (letter (count) of (answer))> add (item (subcount) of [Uppercase v]) to [Results v]   stop script end end

Asking
Using the workaround for the Ask and Wait block, the answer can be forced either uppercase or lowercase.

Editing Scratch
Scratch can be edited to make either two blocks that report the uppercase or lowercase of text, or one that reports text uppercase, lowercase, reversed, shuffled, or exactly as it is inputted.

2 Blocks
Here is how to make the 2 blocks:

Add these blockspecs to the Operators section of

Put these methods in : uppercase: t1         ^ t1 asUppercase lowercase: t1         ^ t1 asLowercase

Here is an explanation of them: uppercase: t1 "defining the method title and variables" ^ t1 asUppercase "reporting the insert as uppercase" Lowercase is the same.

1 Block
Doing it with one block is more advanced, and requires a dropdown menu.

First, create the blocspec in the Operators section of

('%F %s' #r #do:toText:)

Put the method in :

do: t1 toText: t2 t1 = 'reverse' ifTrue: [^ t2 reversed]. t1 = 'shuffle' ifTrue: [^ t2 shuffled]. t1 = 'uppercase' ifTrue: [^ t2 asUppercase]. t1 = 'lowercase' ifTrue: [^ t2 asLowercase]. t1 = 'report' ifTrue: [^ t2]

Then, create the dropdown menu in

Find where it says:  $f = t2 ifTrue: [^ ChoiceArgMorph new getOptionsSelector: #mathFunctionNames; choice: 'sqrt'].

After that, add a line:  $F = t2 ifTrue: [^ ChoiceArgMorph new getOptionsSelector: #doToTextDropdown; choice: 'reverse'].

This will make a dropdown menu that gets the options from the method " ".

Go to  and add the code to make the dropdown:

doToTextDropdown ^ #('lowercase' 'uppercase' 'reverse' 'shuffle' 'report' )

Example Uses
Case sensitivity can be useful in some cases. These include: ask [Please type in your password] and wait if <(answer) = (password)> //case sensitive workaround set [logged in v] to [true] end ask [Please type in some text] and wait set [i v] to [0] repeat (length of (answer)) change [i v] by (1) switch to costume (letter (i) of (answer)) // switch to costume is case sensitive end
 * Password checking in an OS
 * Parsing user input to type