The Uppercase Challenge

The aim of this challenge is to rewrite the upper() function that is used in Python to convert the case of a string to uppercase.

This is how the upper() function works in Python:

For this challenge we will write a function called uppercase() that takes a string as a parameter and returns the uppercase version of this string. However, to complete this challenge we are not allowed to use the pre-defined upper() or lower() Python functions!

Our Python function will need to:

  1. Access each character of the given string
  2. Find out if the character is a lowercase letter of the alphabet
  3. If so, convert this character to its uppercase equivalent. (We will do so using the ASCII code!)
  4. Append the character to a new string
  5. Return the new string once all character have been checked.

ASCII Code

Each character that you can use on your keyboard has a unique ASCII value, a number between 0 and 255. This includes the 26 letters of the lowercase alphabet, the 26 letters of the uppercase alphabet, the number digits 0 to 9 and all the punctuation signs such as !,?.#, etc. Even the space bar has a unique ASCII value: 32.

Here are the ASCII values of the lowercase alphabet:

Character a b c d e x y z
ASCII 97 98 99 100 101 120 121 122

And below are the ASCII values of the uppercase alphabet:

Character A B C D E X Y Z
ASCII 65 66 67 68 69 88 89 90

So, if we have the ASCII value of a lowercase character, we can retrieve the ASCII value of its matching uppercase character by taking away 32 from the ASCII value of the lowercase character.

For instance:

(a) 97 – 32 = 65 (A)

And…

(z) 122 – 32 = 90 (Z)

Python Code and ASCII Code

In Python, you can use the ord() and chr() functions to convert a character into its ASCII value and vice-versa.

For instance, the ord() function is used get the ASCII value of any given character:

The chr() function returns the character for any given ASCII value (number between 0 and 255).

Also, to find if a character is a character from the lowercase alphabet, all we need to do is check that its ASCII value is between 97 and 122.

Finally, remember that in Python, you can easily access each character of a string using a for loop:

Alternative method:

Your Turn…

It is now your turn to code your own uppercase() function in Python, without using the existing upper() or lower() functions!

Remember, your function will take a string as a parameter and will need to:

  1. Access each character of the given string
  2. Find out if the character is a lowercase letter of the alphabet
  3. If so, convert this character to its uppercase equivalent. (We will do so using the ASCII code!)
  4. Append the character to a new string
  5. Return the new string once all character have been checked.

We have started this function for you and you will now need to complete the code to implement the above 5 steps!

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area
Tagged with: