7.19. Coding Practice

A palindrome is a word, phrase, or sentence that reads the same forwards and backwards. Write a function isPalindrome that takes a string input as a parameter and returns a boolean that is true if the input is a palindrome and false otherwise. 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.

Loading a dynamic question ...
Selecting from: cp_7_AC_2q, cp_7_AC_2q_pp

Write a void function censorWord that censors a given word from a given string and prints out the new string. censorWord should take two strings input and word as parameters and prints out input with every occurence of word censored with asterisks. For example, censorWord ("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.

Loading a dynamic question ...
Selecting from: cp_7_AC_4q, cp_7_AC_4q_pp

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 ROT13, 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 ROT13 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.

Loading a dynamic question ...
Selecting from: cp_7_AC_6q, cp_7_AC_6q_pp

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 chars in the string. We capitalize the first character and all characters following a space using toupper. At the end, we return the string.

Loading a dynamic question ...
Selecting from: cp_7_AC_8q, cp_7_AC_8q_pp

Write the function longestWord, which takes a string input as a parameter. longestWord 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 longestWord function. We use a while loop to go through all the chars 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 chars 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.

Loading a dynamic question ...
Selecting from: cp_7_AC_10q, cp_7_AC_10q_pp

You have attempted of activities on this page