How the Scratch Custom Upper and Lower case blocks work

Last week I wrote a post about the custom string handling blocks I have written for Scratch. These have been very well received and I have had quite a few positive tweets back about them. I’m glad people are finding them useful!

Two of the blocks I have written allow you to convert a string to either upper or lower case. Now Scratch 2 when dealing with strings or characters is case insensitive, by this I mean if you perform a comparison of A = a Scratch will evaluate these to be the same. To clarify this, lets look at the block of code below.

Character ComparisonIf you run this block, you will see that the variable result is set to 1. This is what I mean when I say case insensitive, we may consider that A is not the same as a. So what does this have to do with converting our string to upper or lower case? Well, I’ll cover this a little later and you will see how this case insensitivity can help us!


If I was asked to write a very basic case changing method (same as a block in Scratch) in say C# (which is my preferred language), I may well consider looking at ASCII codes for characters, this is ignoring the fact that there is already two methods available in the .NET Framework to convert between upper and lower case!!. What is ASCII? well, I’m not going into the depths of explaining this here, but if you look at the Wikipedia entry for it at this will give you a full overview of it. ASCII codes can be our friend here for converting between case, if you consider the ASCII code for A is 65 (decimal) and the ASCII code for a is 97 (decimal), you can see there is a difference in the code values of 32. This works for B/b and C/c, etc.

So a method to convert upper case to lower case could loop over all the characters in a string and check the ASCII value for it. If the value is between 65 and  90, (A to Z), we can consider it to be an upper case letter, if we then add 32 to the ASCII value, this will give us the lower case version. We want to range check the letter value to ensure we are working with an upper case letter, otherwise the resulting string would look a little strange.

The small C# method below is something that will convert upper to lower case. Now at this point any C# programmer looking at the method would probably pick it to pieces and to be honest I can too. If I was going to write a version for production code it would be vastly different to this. This method has been written to explain my points above and nothing more.

string ToLowerCase(string str)
    string result = String.Empty;

    for (int loop = 0; loop < str.Length; loop++)
        if ((int)str[loop] >= 65 && (int)str[loop] <= 90)
            result += Convert.ToChar((int)str[loop] + 32);
            result += str[loop];

    return result;

Passing the string abcd&ABCD to this method would return the string abcd&abcd. Great! we have a working conversion routine. So lets go and implement this in Scratch.

Our Scratch Version

Right, so now we know we can work with the ASCII codes for letters, we just convert what we have above to the corresponding Scratch code, right? …. Well, no we don’t sadly. Scratch does not give us the ability to work with the ASCII codes for letters, so this method will not work. Great! so how can we do this then? Well, remember I explained at the beginning of this post that Scratch is case insensitive and it can be our friend, well this is where it becomes our best friend!

When I was implementing my string blocks, I really did think that case conversion had to be included, so with a little thought figured there must be a way of doing this. The trick with solving any problem like this is to have a look at what we have to work with in Scratch. Whilst the C# method is very nice, we have to throw most of it away and think again. My eureka moment came when I thought about lists. My thinking here when converting to lower case was as follows:

1. Have a list that contains all the lower case letters a through to z
2. Loop through every character in our string and see if it exists in our list
3. If it exists, use the character found in the list (which will be the lower case character)
4. If it is not found in the list, use the original character.

So, putting this together, I generated a list called LowerCase which looks like this:

Lowercase ListThis method will work even if we pass over lowercase letters, we will still get our character from the list, which will be lower case. So, my Scratch ToLowerCase block looks like this:

ToLowerCase BlockOur block starts by initialising a couple of variables. result will hold our converted string and charCount is used by the repeat block to loop through every character in our string. I have then implemented another custom block called GetLowerCaseLetter. Splitting out code like this into another block makes the code more readable and also gives us yet another block that allows us to do single character conversion. Our GetLowerCaseLetter block looks in our list and finds the lower case letter we need. After calling this block, our lower case letter is held in the variable letterResult so we simply add this to our result variable. We then add 1 to the character count and go around the loop again. This keeps going until we have worked with every character in our string.

The GetLowerCaseLetter Block

This is where most of the work is done. Lets have a look at this block.

GetLowerCaseLetter BlockAgain at the beginning we initialist a couple of variables. loop is used to loop through our LowerCase list. letterResult is used to return our converted character. We are then going to loop through the list a maximum of 26 times (remember a to z!!). When we are in the loop, we check if the letter we have passed to the block, e.g. A is in our list. So using a simple if block we are essentially saying if A = a then set letterResult to the item found in our list, which will be a. As we don’t want to go around the loop again, we stop this script. At this point letterResult holds a which is the lower case version of what we passed in. If we had passed in a it would have found this and letterResult would hold a.

If our block has got as far as the end of the loop, we have another if block that says if letterResult is empty, we must have passed in a character other than a letter of the alphabet, so just simply return it. This ensures anything like %&*() is still kept in the converted string.

So putting this to the test, if we use the block like this:

Call ToLowerCaseThe string we will get back is abcd&abcd, which is the lower case version of what we passed in!

Converting to Upper Case

Converting to upper case is exactly the same, we just use a list that contains upper case A to Z. If you look at the ToUpperCase block in the Scratch project, you will see it is exactly the same as its lower case version. The GetUpperCaseLetter block again is exactly the same as its lower case version.

So, again as you can see, whilst our Scratch version of converting to upper and lower case is very different to what we may do in other programming languages, if you sit back and think about the problem and consider what you have to work with in Scratch, there is generally always a way of implementing what you want to do.


Scratch Custom String Blocks

Having the ability to manipulate strings is one of the most powerful facilities you can have in a programming language. In my day to day coding I manipulate strings an awful lot. You may want to be able to get specific characters in a string, convert the whole string to uppercase characters or lowercase characters. Many years ago, one of the first programming languages I learnt was BASIC ( BASIC has several functions for allowing you to manipulate strings, for example:

LEFT$(string, n)

LEFT$ allows you to return the LEFT most n characters from your string. For example if you use the command PRINT LEFT$(“Scratch”,3) the letters Scr would be printed to the screen. The n parameter tells the function how many characters starting at the beginning of the string you want.

RIGHT$(string, n)

Similar to LEFT$, RIGHT$ allows you to return the RIGHT most n characters from your string. So something like PRINT RIGHT$(“Scratch”, 3) would print tch to the screen. The n parameter tells the function how many characters starting at the end of the string and working backwards you want.

MID$(string, start, n)

MID$ is quite a powerful function. It allows you to get a certain amount of characters, but starting at any point in the string. So using a command like PRINT MID$(“Scratch Programming”, 9, 7) would print Program to the screen. The start parameter tells the function where you want to start in the string and the n parameter tells the function how many characters you want to return from the starting point.


Some other functions available are UPPER$ and LOWER$. These return either an uppercase or lowercase version of your string. So something like PRINT UPPER$(“scratch”) would print SCRATCH and PRINT LOWER$(“SCRATCH”) would print scratch. Again, very useful!

How can we do this in Scratch?

Out of the box, Scratch does not have these kind of blocks built in. As we know, Scratch 2 allows us to write our own custom blocks, so we could write our own versions of these. What I have done is build versions of the functions explained above as custom blocks that can be used in your Scratch programs.

I have made these available at If you look inside the project, all the custom blocks are defined on the stage, with an example of how you would use each block next to the block definition. I will given an explanation below on how you would use each custom block.

Please Note: The resulting string from each of these blocks is stored in a variable called ‘result’.

Scratch LEFT$

This has been implemented as a custom block called Left. You would use the block like this:

Scratch Left BlockThe first parameter is the string or the variable holding the string you want to work with. The second parameter is the number of characters from the left you want. The result is stored in a variable called result. Looking at the example above, after calling this block, the result variable would hold Scr.

Scratch RIGHT$

This has been implemented as a custom block called Right. You would use the block like this:

Scratch Right BlockThe first parameter is the string or variable holding the string you want to work with. The second parameter is the number of characters from the right that you want. The result is stored in a variable called result. Looking at the example above, after calling this block, the result variable would hold tch.

Scratch MID$

This has been implemented as a custom block called Mid. You would use the block like this:

Scratch Mid BlockWe have three parameters that we have to supply to this block. The first parameter is the string or variable holding the string you want to work with. The second parameter is where you want to start in the string. The final parameter is the number of characters you want to get from the string. The result is stored in a variable called result. So, looking at the example call above, we can see that we will be working with the string Scratch Programming and we want to start returning characters from the 9th character in the string and that we want to return 7 characters. Starting at the 9th character (P) we will get 7 characters, which will give us the string Program.

Scratch UPPER$ and LOWER$

I have also implemented blocks that allow strings to be converted to upper of lower case. These blocks are called ToUpperCase and ToLowerCase. As with all the other blocks, the result is stored in a variabled called result. You would use the blocks like this:

Scratch ToUpperCase BlockScratch ToLowerCase BlockCalling the ToUpperCase block on the string shown would convert it to SCRATCH and calling the ToLowerCase block on the string shown would convert it to scratch. If you supplied a string such as ScRaTcH to either of these blocks, it will still work as expected.

I am going to write a further blog post on how the ToUpperCase and ToLowerCase blocks work, because Scratch is a little different to most languages when comparing upper and lower case characters. The code for the other blocks is fairly easy to follow and figure out whats going on.

Anymore Blocks?

I will be implementing more string manipulation blocks over a period of time. The blocks I want to implement next are:

  – SPLIT(string, char)
  – LTRIM(string)
  – RTRIM(string)
  – TRIM(string)

SPLIT enables you to split one string into several strings, splitting the string at a specific character. For example if you supplied a string I Program In Scratch and told the block to split on the space character, you would end up with 4 strings containing:


Each of these strings would be held in a list.

LTRIM and RTRIM removes all spaces to either the left or the right of a string. So if you had a string ”    Scratch” (quotes used to show the spaces) and passed the string to the LTRIM block, it would remove all the leading spaces. Similarly, if you had the string “Scratch   ” and passed the string to the RTRIM block, it would remove all the trailing spaces. TRIM performs a union of LTRIM and RTRIM and will remove all leading and trailing spaces.

As these blocks are added, I will add a post to this blog and will also add a comment in the Scratch forums and on the ScratchEd web site.

Use and Share!

I do hope that you find these blocks useful and feel free to use and modify them in any way you would like. Share them around with your fellow Scratchers.

The Power of Twitter – Crazy Weather in Falmouth

Hail Storm in September I’ve been a Twitter user for quite a while now and have to say I do think its a great invention. I mainly use it for fun, but do follow people related to the IT world, and as I travel frequently, follow an awful lot of the travel update feeds. Yesterday it was a great little tool for showing just how strange things can get!!

We had a very bizarre weather event here in Falmouth yesterday at about 5pm. The picture to the left shows what it looked like here from my house. Now considering its September and we have had one of the hottest summers for 7 years, you don’t expect to look out of your window and see the whole place white with cars struggling to get up the road! We’d had a severe weather warning put in place for heavy rain, but for 45 minutes we had one of the heaviest hail storms I have ever seen.

Watching the roads get worse and cars struggling to get up the hill was getting a little concerning, considering our road can get busy (well busy for an estate road anyway!!). What did it for me was watching a 4×4 skid up the road from one side of the road to the other and almost hit a car that had stopped as it couldn’t get any further up the hill. I said to my wife, I think I will tweet BBC Travel South West (@BBCTravelSW) about this. BBC Travel South West is fantastic and I always look at the updates from them on Twitter before I do a long journey.

The Power of Twitter!

I tweeted them with the picture above telling them where it was and how bad it was. Within a few minutes, they had re-tweeted, and they have one or two followers! I also follow a local weather feed as well, Kernow & Channel Islands Weather (@kernowweather – As they like to know about strange weather events around Cornwall I tweeted them the picture. I didn’t think anything more about it until my phone started pinging off twitter notifications. There was certainly a fair amount of re-tweeting going on and I was glad people were enjoying the picture.

Deciding to have an early night I was reading my book (yes the phone was on the bedside table) and Twitter started again. This time I was being tweeted by a Journalist from the BBC and from a newspaper in Devon asking if they could use the picture on their sites, feel free I said!!

Kernow and Channel Islands Weather then tweeted back saying thanks for the picture and they have a full weather report on their site with my little snap of the hail. My wife then comes down this morning and says, “Is that your picture on the Western Morning News website?” ( — Really?? Quick trip over to their website and yes, there it is with a report on the weather.

Whats the big deal you posted a picture on Twitter!

Yep, so what, its only a picture! My point here is that whilst Twitter has had a bit of a bashing one way or another in the news, its down to what you post for the world of Twitter to see. Whilst I only originally posted the picture to BBC Travel South West to let them know what was happening in Falmouth at the time, to give warning to people travelling or wanting to travel in the Falmouth direction, judging by some of the tweets I’ve had back people have enjoyed seeing the picture and a couple of the news papers seem to like it too. This kind of freak weather event doesn’t happen very often and its a great way to record and share it with others.

Happy tweeting!


First Simple App for Windows Phone 8

Having had my Windows Phone for a couple of months now, and having set up a Windows 8 virtual machine to do some app development, I finally decided to sit down and bang out a simple app, to see how easy it is.

To be honest if your skill set follows the form of C#, WPF/Silverlight you won’t have many problems at all. You will need to have Visual Studio 2012 and the Windows Phone 8 SDK installed, but thats a fairly easy (but somewhat lengthy!) process to complete.

As I do a fair amount of driving and tutor Advanced Driving, for my first app I decided to put together a Stopping Distance calculator. I’ve done these for Android and BlackBerry, so thought I would do it for Windows Phone as I have all the calculations to hand.

The idea of the calculator is a simple one. Supply your speed in Miles per Hour or Kilometres per Hour and it will calculate the Braking Distance, Thinking Distance and the overall Stopping Distance (Thinking+Braking). I’ve added a couple of screen shots below of the app running in the emulator. It certainly seems to run fine, and the whole process of developing it from start to finish was about 2 hours. Not bad for a first app.

Screen 1 – Inputs

The inputs into the app are very simple, your speed and from the list, select your unit of measure (MPH or KPH). This is a list picker and will expand when you want to make your selection and then contract when you have made the selection. Simply then tap the Calculate button and you are taken to the results page showing the calculation results.

App Screen 1Screen 2 – Results

The Thinking Distance, Braking Distance and overall Stopping Distance is then displayed with the results in metres and feet. You can then tap another button to take you back to the input screen to allow you to calculate using another speed/unit of measure.

app screen 2

A far from complicated app, but someone going through a driving test, or Advanced driving test does need to know stopping distances. A simple little app that can aid revision of stopping distances.

I think I’ll tidy up the screens, make them a little more visually appealing and then upload to the Windows Phone App Store.