7.19. Coding Practice¶
A palindrome is a word, phrase, or sentence that reads the same forwards and backwards.
Write a function is_palindrome
that takes a string input
as a parameter and returns
a boolean that is true if the input is a palindrome and false otherwise.
The tests skip non-alphanumeric characters (space, puncuation, etc).
Run and test your code!
Below is one way to implement the program. We use the isalpha
function
to ignore the non alphabetical characters. Then we continuously check to see
if the letters in the front are equal to the ones in the back until we reach the
middle of the string.
How much does Bubba love shrimp? Probably a lot. But how many times does the word “shrimp” come
up in his monologue? Write a function count_word
that counts the number of times a given word
appears in a given string. count_word
should take two strings input
and word
as parameters and return an int
.
Feel free to use the stringToLower
function we wrote earlier.
Write a void function censor_word
that censors a given word from a given string and prints
out the new string. censor_word
should take two strings input
and word
as parameters
and prints out input
with every occurence of word
censored with asterisks. For example,
censor_word ("I really, really, really, really, really, really like you", "really")
results in
the following output:
I ******, ******, ******, ******, ******, ****** like you
Below is one way to implement the program. We use a while loop to repeatedly search for instances of word in input. Once found, we replace the length of the word with asterisks.
Write a void function remove_word
that removes a given word from a given string and prints
out the new string. remove_word
should take two strings input
and word
as parameters
and prints out input
with every occurence of word
removed. Use string concatenation and the C++
string function substr
. substr
takes two parameters, a starting index and a length. For example,
if string greeting = "hello world"
, then greeting.substr(6, 5)
returns the string "world"
.
Test your function in main. The output should be:
Gucci , Gucci , Gucci , Gucci
ROT13 is a simple letter substitution cipher that shifts every letter forward by 13,
looping around if necessary. For example, the letter ‘a’, 1st in the alphabet, becomes
the letter ‘n’, 14th in the alphabet. The letter ‘r’, 18th in the alphabet, becomes the
letter ‘e’, 5th in the alphabet. Since the alphabet has 26 letters and 13 is exactly half,
a message encrypted using ROT13 can be decrypted by calling ROT13 on the encrypted message.
Write the function rotate13
, which takes a string input
as a parameter and returns
an encrypted string
. Test your function in main
.
Below is one way to implement the rotate13
function. We use a while
loop to
go through all the letters in the string
. If the letter is between ‘a’ and ‘n’ or
‘A’ and ‘N’, we use character operations to add 13 to each letter. Otherwise,
we subtract 13 from each letter. We return the encrypted message at the end.
Write the function reverse_word
which takes a string input
, reverses it,
and returns the reversed string
. Run and test your code!
Write the function capitalize
, which takes a string input
as a parameter.
capitalize
capitalizes the first letter of every word, and returns the new string
.
Below is one way to implement the capitalize
function. We use a while
loop to
go through all the char
s in the string
. We capitalize the first character
and all characters following a space using toupper
. At the end, we return the string
.
Write the function count_vowels
which takes a string input
and returns
the number of vowels in the string
.
For this exercvise, ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’ are vowels.
Run and test your code!
Write the function longest_word
, which takes a string input
as a parameter.
longest_word
returns the words with the most letters in input
. If there’s a tie,
return the first word. Use the substr
function. Run and test your code!
Below is one way to implement the longest_word
function. We use a while
loop to
go through all the char
s in the string
. We use variables to keep track of the
longest word, the longest amount of letters, and the length of the current word. We
can determine the length of a word by counting the number of char
s between spaces.
If the length is greater than the max, length becomes the new max and we update the longest word.
This keeps repeating until we reach the end of the string, and the longest word is returned.
Camel case is the practice of writing phrases without spaces or punctuation,
indicating the separation of words using capital letter. For example, “camel case”
in camel case is “camelCase”. Snake case is the practice of writing phrases
where each space is replaced by an underscore. For example, “snake case”
in snake case is “snake_case”. Write the functions snake_to_camel
and camel_to_snake
.
Each function takes a string input
and returns the input using the other stylization.
Feel free to use any string
functions you’d like. Run and test your code!