From Test-Scratch-Wiki

This article or section documents an outdated version of Scratch (version 1.4). For this article in Scratch 2.0, see Eng:Case Sensing (2.0).

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:

  • Uppercase
  • Lowercase
  • Results

A variable called "count" will also be needed.

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

Note Note: This is for advanced Scratchers and Squeak users only, and projects made like this will not work if uploaded.
The blocks that can be made.

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 Scratch-Objects >> ScriptableScratchMorph class >> blockspecs >> blockspecs: (See more about blockspecs here.)

(With the () = (), () < (), and () > () blocks:) ('%s is %s' #b #case:sensitive:)
(In a new section (achieved by typing #- outside of a blockspec.) ): ('%s is lowercase' #b #isLowercase:) ('%s is uppercase' #b #isUppercase:) ('%s is mixed upper/lower-case' #b #mixedCase:)

Then, in Scratch-Objects >> ScriptableScratchMorph instance >> string ops, 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 [[Eng:#Using Lists|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

Note Note: This is for advanced Scratchers only, and projects made like this will not work if uploaded.

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 Scratch-Objects >> ScriptableScratchMorph class >> blockspecs >> blockspecs:

('uppercase %s' #r #uppercase:) ('lowercase %s' #r #lowercase:)

Put these methods in Scratch-Objects >> ScriptableScratchMorph instance >> string ops:

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 Scratch-Objects >> ScriptableScratchMorph class >> blockspecs >> blockspecs:

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

Put the method in Scratch-Objects >> ScriptableScratchMorph instance >> string ops:

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 Scratch-Blocks >> CommandBlockMorph >> private >> uncoloredArgMorphFor:

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 (ChoiceArgMorph) that gets the options from the method "doToTextDropdown".

Go to Scratch-Objects >> ScriptableScratchMorph instance >> string ops 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:

  • Password checking in an OS
ask [Please type in your password] and wait
if <(answer) = (password)> //case sensitive workaround
 set [logged in v] to [true]
end
  • Parsing user input to type
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
Cookies help us deliver our services. By using our services, you agree to our use of cookies.