Homework 2: String Info (20 pts)
Let's create a program to answer the following questions about a string:
- How many characters are in the string?
- Does the string contain a certain character?
- Does the string have any uppercase characters?
- What are all the vowels in the string?
- Does a string contain another string (a substring)?
- Where is a certain character located in the string?
- What is every other character in the string?
- Is the string a palindrome?
- What is the longest word in a string?
Print the results of each function call in the main function only. None of the functions that you define below should use the print statement.
You may NOT use the Python len(), find(), rfind(), or lfind() functions.
Setup
- Locate your "CS102" folder
- Create a folder inside "CS102" called "hw02"
- Download hw02.py
- Move the "hw02.py" file into your hw02 folder
- Open "hw02.py" with Thonny
- Submit hw02.py to Moodle when you are finished
Part I: Length (1 pt)
- Write a function named
length
that takes a string as a parameter
- Count each character in the string parameter
- Return the character count
- In the main function call your
length
function on the string "Hello World"
Part II: Has Character (2 pts)
- Write a function called
has_character
that takes a string and a single character as parameters
- Check if the character matches at least one character in the string
- If the character is found in the string return
True
- If the character was not found in the string return
False
- In the main function call your
has_character
to check for the character "d" in "Hello World"
Part III Has Upper (2 pts)
- Write a function called
has_upper
that takes a string as a paramter
- Check that each character is between 'A' and 'Z' (the capital letters)
- Return
True
if any letter is capital and False
otherwise
- In the main function call your
has_upper
on the string "hello world"
Part IV: Find Vowels (1 pt)
- Write a function called
find_vowels
that takes a string as a parameter
- Check each character to see if it is an 'a', 'e', 'i', 'o', 'u' (a vowel)
- Make sure that it works for both lower and upper case letters
- If the characer is a vowel, concatenate it to a new string containing all the vowels you have found
- The original case of the letter should be preserved
- Example:
find_vowels('HI ThEre')
should return IEe
- Return the string of vowels
- In the main function call your
find_vowels
on the string "Hello World"
- Make another function call to
find_vowels
on the string "COmputErScience"
- HINT: Chapter 3.2
Part V: Finding Substring (1 pt)
- Write a new function called
find_substring
that takes two string parameters
- Check to see if the second string parameter is present in the first string parameter
- Example: "foobar" contains the substring "foo"
- Return
True
if the substring is found otherwise, return False
- In the main function call your
find_substring
on the string "Hello World" to find "lo Wo"
Part VI: Is Palindrome (4 pts)
- Make a function called
is_palindrome
that takes a string parameter
- A palindrome is a string that is the same when read left to right or right to left
- Use what you have learned to determine if the string is a palindrome
- return
True
if the string is a palindrome, or False
otherwise
- In the main function call your
is_palindrome
on the string "racecar"
- Now make your function work if you wanted to be able to ignore spaces
- In the main function call your
is_palindrome
function on at least one of the these examples:
- "taco cat", "rat star", "evil olive"
- HINT: Chapter 3.2
Part VII: Find Character (2 pts)
- Write a function called
find_character
that takes a string and a single character as parameters
- Check if the character is in the string
- Return the position of the first occurance of that character (its index) in the string
- If the character does not exist in the string, return -1
- In the main function call your
find_character
to find the character "r" in the string "Hello World"
- HINT: Chapter 3.2
Part VIII: Skip (4 pts)
- Make a function called
skip
that takes a string parameter
- This function will return a new string made up of every other character in the string starting with the first one.
- Examples
skip('abcde')
will return the string ace
- HINT: Chapter 3.2
- HINT: Use your length function (Part 1)
Part IX: Longest String (3 pts)
- Make a function called
longest_string
that takes a string parameter
- Check the length of each word in the string and and return the longest word
- A word is any sequence of characters separated by a space
- In the main function call your
longest_string
on the string "Computer Science is neat!"
- HINT: Chapter 3.3