Skip to main content

Exercises 9.16 Exercises

1.

Build the function censorLetter which takes a string and a char to censor as parameters and returns a “censored” string where all copies of the char are replaced with *. For example, `censorLetter("Bye world", ’o’)` returns the string “Bye w*rld”.

2.

Palindromes are symmetrical strings. That is a string that reads the same forwards and backwards. palindromes: “hih”, “i”, “bob”, “tenet”, “soos”, “madam” . not palindromes: “join”, “hat”, “frat”, “supper”, “rhythm”.
Let’s write a function called ispalindrome which takes a string named input and returns a bool. We will do so by using two indexes. One that starts at 0 and counts up. The other will start at the end of the string and count down. The function returns true if the string is a palindrome and false if not. The code is mixed up and contains extra blocks. Put the necessary blocks in the correct order.

3.

Complete the code to print the first, middle, and last characters of the string read as input. We will only use odd length strings as the inputs.
Hint.
Use .size() to figure out the length of the string. You can use that to figure out the middle and end position.

4.

Write a bool caturdayCheck(string word) function. It should return true if the word has "cat" starting at either the first or second character of the word. Otherwise, return false.
For example:
Input: catch
Output: true

Input: scatter
Output: true

Input: concatenate
Output: false
Includes and tests are present but hidden.
Hint.
You should not need to check letter by letter... use a string function that allows you to ask "where is this substring within the string?". Use its answer to decide whether to print true or false

5.

Write a string wordShortener(string startWord) function. If the startWord is 4 characters or less, return it. If it is more than 4 characters, return a new string that has the first two characters of the word followed by the last two characters.
For example:
Input: cs
Output: cs
Input: programming
Output: prng
Includes and tests are present but hidden.
Hint.
substr is useful for getting the two chunks. If the string has length n, what index is the next to last character at?

6.

Write the function boldContents. It takes a string that contains [b]???[/b] where ??? can be any text. It should return a string containing just the ??? part.
For example:
Input: [b]This is some text[/b]
Output: This is some text

Input: Hello [b]class[/b].
Output: class
Includes and tests are present but hidden.
Hint.
You can use .find() to locate the start and end. You will need to do math to figure out how many characters are between them.

7.

Write an string initials(string name) function that takes a name like Sergio Romas Garcia or Jane Goodall and returns the initials for that name (SRG or JG). You should use spaces to identify name parts. You can assume there are always either one or two spaces.
Hint 1.
Start by finding the first space. Then try finding a second space. Print out the locations.
Hint 2.
Use a comparison with string::npos to check if the second space actually exists. You will need one set of logic for if there is one spaces and a different set if there are two spaces.

8.

Write a function int countQuotes(string s). It should count how many quotation marks (either " or ') there are in the string and return the total.
For example: if your function is given the input Hello "world" 'this' is "a" test it should return 6 because there are six quotation marks in that string (three double quotes and three single quotes). Similarly, if given the input It's a "small" world it should return 3 (one single quote and two double quotes).
Hint.
You will need to loop through the characters and count the quote symbols. Start by just printing the characters one by one. Then try to just print the quotes. Then count them.

9.

Write a function string leetspeak(string s). It should take the input string, and replace all the e characters with 3, all the o characters with 0, and all the l characters with 1. Then return the modified version of the string. For example:
Input: hello world
Output: h3110 w0r1d
Hint.
You will need to loop to iterate through the string. You can either modify the string you start with or build up a new string from scratch.

10.

Write a function string stringCompactor(string s). It should take the input string and create a “compacted” version where only ever other character is kept (1st, 3rd, 5th,...). For example:
Input: hello
Output: hlo

Input: This is a test
Output: Ti sats
Hint.
You will need a loop. The easiest way to do this is to build up a new string. But you could keep calling erase to delete the characters you do not want from the original string.
You have attempted of activities on this page.