7.19. Coding PracticeΒΆ
Q1
Question
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!
1#include <cctype>
2#include <iostream>
3#include <string>
4
5
6bool is_palindrome (std::string input) {
7 // Write your implementation here.
8}
Answer
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.
1#include <cctype>
2#include <iostream>
3#include <string>
4
5bool is_palindrome (std::string input) {
6 int front = 0;
7 int back = input.size() - 1;
8 while (front < back) {
9 while (!std::isalpha(input[front])) {
10 ++front;
11 }
12 while (!std::isalpha(input[back])) {
13 --back;
14 }
15 if (input[front] != input[back]) {
16 return false;
17 }
18 ++front;
19 --back;
20 }
21 return true;
22}
Q2
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 string_to_lower function we wrote earlier.
1#include <cctype>
2#include <iostream>
3#include <string>
4
5
6void string_to_lower (string &input) {
7 int i = 0;
8 while (i < input.size()) {
9 if (std::isalpha(input[i]) != 0 && std::isupper(input[i]) != 0) {
10 input[i] = std::tolower(input[i]);
11 }
12 ++i;
13 }
14}
15
16int count_word (string input, string word) {
17 // Write your implementation here.
18}
19
20int main() {
21 string quote =
22 "Anyway, like I was sayin', shrimp is the fruit of the sea. You can "
23 "barbecue it, boil it, broil it, bake it, saute it. Dey's uh, "
24 "shrimp-kabobs, shrimp creole, shrimp gumbo. Pan fried, deep fried, "
25 "stir-fried. There's pineapple shrimp, lemon shrimp, coconut shrimp, "
26 "pepper shrimp, shrimp soup, shrimp stew, shrimp salad, shrimp and "
27 "potatoes, shrimp burger, shrimp sandwich. That- that's about "
28 "it.";
29 cout << "Your output: " << count_word(quote, "shrimp") << ", Correct output: 14" << '\n';
30}
Q3
Question
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
1#include <iostream>
2#include <string>
3using std::string;
4
5void censor_word (string input, string word) {
6 // Write your implementation here.
7}
8
9int main() {
10 censor_word ("I really, really, really, really, really, really like you", "really");
11}
Answer
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.
1#include <iostream>
2#include <string>
3using std::string;
4
5void censor_word(string input, string word) {
6 int length = word.size();
7 while (input.find(word) != std::string::npos) {
8 int index = input.find(word);
9 int i = 0;
10 while (i < length) {
11 input[index + i] = '*';
12 ++i;
13 }
14 }
15 std::cout << input;
16}
17
18int main() {
19 censor_word ("I really, really, really, really, really, really like you", "really");
20}
Q4
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
1#include <iostream>
2#include <string>
3
4void remove_word (std::string input, std::string word) {
5 // Write your implementation here.
6}
7
8int main() {
9 remove_word ("Gucci gang, Gucci gang, Gucci gang, Gucci gang", "gang");
10}
Q5
Question
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.
1#include <cctype>
2#include <iostream>
3#include <string>
4
5std::string rotate13 (std::string message) {
6 // Write your implementation here.
7}
8
9int main() {
10 using std::cout;
11 string original = "Encrypt me then decrypt me!";
12 string encrypted = rotate13 (original);
13 string decrypted = rotate13 (encrypted);
14 cout << "Original string: " << original << '\n';
15 cout << "Encrypted string: " << encrypted << '\n';
16 cout << "Decrypted string: " << decrypted << '\n';
17
18 // Uncomment and run the code below once your function works!
19 // string secret_message = "Pbatenghyngvbaf! Lbh'ir fhpprffshyyl vzcyrzragrq EBG13 naq qrpbqrq gur frperg zrffntr :)";
20 // cout << rotate13 (secret_message) << '\n';
21}
Answer
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.
1#include <cctype>
2#include <iostream>
3#include <string>
4using std::string;
5
6string rotate13(string message) {
7 size_t pos = 0;
8 while (pos < message.size()) {
9 char& letter = message[pos];
10 if (std::isalpha(letter) != 0) {
11 if ((std::islower(letter) != 0 && letter < 'n') ||
12 (std::isupper(letter) != 0 && letter < 'N')) {
13 letter = letter + 13;
14 } else {
15 letter = letter - 13;
16 }
17 }
18 ++pos;
19 }
20 return message;
21}
22
23int main() {
24 using std::cout;
25 string original = "Encrypt me then decrypt me!";
26 string encrypted = rotate13 (original);
27 string decrypted = rotate13 (encrypted);
28 cout << "Original string: " << original << '\n';
29 cout << "Encrypted string: " << encrypted << '\n';
30 cout << "Decrypted string: " << decrypted << '\n';
31
32 string secret = "Pbatenghyngvbaf! Lbh'ir fhpprffshyyl vzcyrzragrq EBG13 naq qrpbqrq gur frperg zrffntr :)";
33 cout << rotate13 (secret) << '\n';
34}
Q6
Write the function reverse_word which takes a string input, reverses it,
and returns the reversed string. Run and test your code!
1#include <string>
2
3std::string reverse_word (std::string input) {
4 // Write your implementation here.
5}
Q7
Question
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.
1#include <cctype>
2#include <iostream>
3#include <string>
4
5std::string capitalize (std::string input) {
6 // Write your implementation here.
7}
8
9int main() {
10 std::cout << capitalize ("every word in this string should be capitalized!\n");
11 std::cout << capitalize ("this String As well\n");
12}
Answer
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.
1#include <cctype>
2#include <iostream>
3#include <string>
4
5std::string capitalize (std::string input) {
6 size_t pos = 0;
7 while (pos < input.size()) {
8 if (pos == 0) {
9 input[pos] = std::toupper(input[pos]);
10 }
11 else if (input[pos-1] == ' ') {
12 input[pos] = std::toupper(input[pos]);
13 }
14 ++pos;
15 }
16 return input;
17}
18
19int main() {
20 std::cout << capitalize ("every word in this string should be capitalized!\n");
21 std::cout << capitalize ("this String As well\n");
22}
Q8
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!
1#include <string>
2
3int count_vowels (std::string input) {
4 // Write your implementation here.
5}
Q9
Question
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!
1#include <string>
2
3std::string longest_word (std::string input) {
4 // Write your implementation here.
5}
Answer
Below is one way to implement the longest_word 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.
1#include <string>
2
3std::string longest_word (std::string input) {
4 size_t pos = 0;
5 std::string longest;
6 int max_length = 0;
7 while (pos < input.size()) {
8 int word_length = 0;
9 while (input[pos] != ' ' && pos < input.size()) {
10 ++word_length;
11 ++pos;
12 }
13 if (word_length > max_length) {
14 max_length = word_length;
15 longest = input.substr(pos - max_length, max_length);
16 }
17 ++pos;
18 }
19 return longest;
20}
Q10
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!
1#include <string>
2using std::string;
3
4string snake_to_camel (string input) {
5 // Write your implementation here.
6}
7
8string camel_to_snake (string input) {
9 // Write your implementation here.
10}