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 .length() 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.

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.
substrn

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.
.find()

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.
Hint 2.
string::npos

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. Don’t forget you need to use escape codes to represent the characters " and '. For example:
Input: Hello "world" 'this' is "a" test
Output: 6
Hint.

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.

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.
erase
You have attempted of activities on this page.