11A: Lower Case
Source: https://cscircles.cemc.uwaterloo.ca/11a-lower-case/ Parent: https://cscircles.cemc.uwaterloo.ca/
Lesson 11 has three parts A, B, C which can be completed in any order.
This lesson contains an exercise where you need to write two functions: one will use the other to accomplish its goal. The goal is to eventually write a function lowerString that can convert all of the letters in a string to lower case. (A, B, C are upper case letters and a, b, c are lower case.) For example, the result of
lowerString("This string has 9 CAPITAL letters (& Punctuation)!")
should be
"this string has 9 capital letters (& punctuation)!"
Step 1: Characters
The first step is to write a function lowerChar(char) that can return the result of converting a single character char to lower case. It should do the following:
- if the input character
charis a capital letter (between 'A' and 'Z'), it should return the lower-case version of the letter (between 'a' and 'z') - in all other cases, it should return the same
charwhich was input.
(In order to do the first step, you will have to use an if statement, an and operator, and apply some knowledge from the lesson about strings.)
Coding Exercise: Lower-case Characters
Define a function lowerChar(char) which meets the above description.
You need to create an account and log in to ask a question.
delete this comment and enter your code here
Enter testing statements like print(myfunction("test argument")) below.
More actions... History Help
Step 2: Strings
Now, you will write a second function lowerString(string) which will return the result of converting the entire string to lower case, by calling lowerChar on each character. We suggest you do this as follows:
- first, copy the definition of
lowerChar(char)from your solution to the first part - then define a second function,
lowerString(string) - on the first line inside lowerString, initialize a variable
result = ""equal to the empty string - use a for loop with
iand setresult = result + lowerChar(string[i]) - finally,
return result
Coding Exercise: Lower-case Strings
Define a function lowerString(string) which returns the result of converting string to lower case.
You need to create an account and log in to ask a question.
first, copy your definition of lowerChar() here
then define lowerString(string)
Enter testing statements like print(myfunction("test argument")) below.
More actions... History Reset code to default Help
Later on, you will learn about the string.lower() method, which is a built-in way to perform this task. |