7.18. Mixed Up Code Practice

Q1

Write a program that prints the 4th character of word, and finds and replaces all instances of 'i' with 'e'. Finally, print out the string. Put the necessary blocks in the correct order.

  1. cout << irritating[3] << '\n';  #distractor
  2. cout << word << '\n';
  3. cout << word.at(4) << '\n';  #distractor
  4. cout << word[3] << '\n';
  5. cout << word[4] << '\n';  #distractor
  6. int main() {
  7. string word = "irritating";
  8. while ((int)word.find('e') != -1) {  #distractor
  9. while ((int)word.find('i') != -1) {
  10. while ((int)word.find('i')) {  #distractor
  11. word[word.find('e')] = 'i';  #distractor
  12. word[word.find('i')] = 'e';
  13. }
  14. }

Q2

An anagram is a play on words by rearranging the letters of the original words to form new words. For example, the letters in "listen" can be rearranged to make "silent". Write a program that rearranges "night" into "thing" and prints the anagram. Put the necessary blocks in the correct order.

  1. anagram[0] = original[original.find('t')];
  2. anagram[1] = original[original.find('h')];
  3. anagram[2] = original[original.find('i')];
  4. anagram[3] = original[original.find('n')];
  5. anagram[4] = original[original.find('g')];
  6. cout << anagram;
  7. int main() {
  8. string anagram = original;
  9. string original = "night";
  10. string original = "thing";
  11. }

Q3

Let's write the function longer_string, which takes two parameters, first and second. If first has more letters than second, longer_string prints "first is longer than second", and vice versa. If they have the same number of letters, longer_string prints "first and second are the same length". Put the necessary blocks in the correct order.

  1. cout << first << " and " << second << " are the same length" << '\n';
  2. cout << first << " is longer than " << second << '\n';
  3. cout << second << " is longer than " << first << '\n';
  4. cout << second << " is longer than " << second << '\n';
  5. else (first.length() == second.length()) {  #distractor
  6. else if (first.length() < second.length()) {
  7. else {
  8. if (first.length() > second.length()) {
  9. if (first.length() >= second.length()) {
  10. string longer_string (string first, string second) {
  11. void longer_string (string first, string second) {
  12. }
  13. }
  14. }
  15. }

Q4

Let's write the code for the cipher_text function. cipher_text should be a void function that takes input as a parameter, increases the value of each character by 1 (i.e. "bad" turns into "cbe"), and prints the encrypted string.

  1. cout << input;
  2. i++;
  3. input[i] = input[i] + 1;
  4. input[i] = input[i] - 1;
  5. int i = 0;
  6. return input;
  7. string cipher_text (string input) {
  8. void cipher_text (string input) {
  9. while (i < input.length() - 1) {
  10. while (i < input.length()) {
  11. }
  12. }

Q5

The program below should print out the number of occurences of the character 't' in the string tongue_twister but the code is mixed up. Put the necessary blocks in the correct order, with declaration in the order of tongue_twister, count, and i.

  1. count++;
  2. cout << count;
  3. i++;
  4. if (tongue_twister[i] = 't') {
  5. if (tongue_twister[i] == 't') {
  6. int count = 0;
  7. int count = 1;
  8. int i = 0;
  9. int main() {
  10. string tongue_twister = "twelve twins twirled twelve twigs";
  11. while (i < (int)tongue_twister.length()) {
  12. }
  13. }
  14. }

Q6

The program below should print out the index of the second instance of the character 'i' but the code is mixed up and contains extra blocks. Put the necessary blocks in the correct order.

  1. cout << first;
  2. cout << index;
  3. int first = quote.find('i');
  4. int i = 0;  #distractor
  5. int index = find (quote, 'i', first + 1);
  6. int index = find (quote, 'i', first);
  7. int main() {
  8. string quote = "Your time is limited, so don't waste it living someone else's life.";
  9. while (i < quote.length()) {  #distractor
  10. }

Q7

Deep in the forest live the 7 dwarves named Sorty, Torty, Vorty, Worty, Xorty, Yorty, and Zorty. The program below should print out each of their names but the code is mixed up and contains extra blocks. Put the necessary blocks in the correct order.

  1. char letter = 'S';
  2. cout << letter + suffix << '\n';
  3. if (letter != 'U') {
  4. if (letter == 'U') {
  5. int main() {
  6. letter++;
  7. string name = “Sorty”; #distractor
  8. string suffix = "orty";
  9. suffix++;
  10. while (letter <= 'Z') {
  11. }
  12. }
  13. }

Q8

On the strange planet of Noes, there's a law that prohibits the usage of the letter "e". As a result, they hired you to write a function called censor_e that replaces all occurences of the letter "e" in a string with an asterisk and returns the censored string. For example, if the input is "hello world", the function returns "h*llo world".

  1. '*' = input[i];
  2. i++;
  3. if (input[i] = 'e') {
  4. if (input[i] == 'e') {
  5. input[i] = '*';
  6. int i = 0;
  7. return input;
  8. string censor_e (string input) {
  9. string copy = input;  #distractor
  10. void censor_e (string input) {
  11. while (i < input.length() - 1) {
  12. while (i < input.length()) {
  13. }
  14. }
  15. }

Q9

Your work for the planet of Noes impressed the nearby planets of Noas, Nois, Noos, and Nous. They want you to write different functions that censor out each planet's corresponding forbidden letter. However, your galaxy brain knows better than to write a different function for each planet. Using generalization, write the function censor_letter which takes input and a char to censor as parameters and returns a censored string. For example, censor_letter("Bye world", 'o') returns the string "Bye w*rld".

  1. '*' = input[i];
  2. i++;
  3. if (input[i] == "letter") {
  4. if (input[i] == letter) {
  5. input[i] = '*';
  6. int i = 0;
  7. int i = 1;
  8. return input;
  9. string censor_letter (string input) {
  10. string censor_letter (string input, char letter) {
  11. while (i < input.length()) {
  12. }
  13. }
  14. }

Q10

Let's write a function called alpha_combine which takes two strings, first and second, and returns a string which concatenates first and second in alphabetical order. For example, alphabetizer ("zebra, mega") returns the string "megazebra" since "mega" comes before "zebra" in the alphabet. Put the necessary blocks in the correct order.

  1.    cout << second << first;  #distractor
    }
  2. cout << first << second;
  3. else {
  4. if ("first" > "second") {
  5. if (first > second) {
  6. return first + second;
  7. return second + first;
  8. string alpha_combine (string first, string second) {
  9. void alpha_combine (string first, string second) {
  10. }
  11. }

Q11

Let's write a function called <code>ispalindrome</code> which takes a <code>string</code> named input and returns a <code>bool</code> The function returns true if the <code>string</code> is a palindrome and false if not. palindromes are symmetrical strings. That is a string that reads the same backwards is palindrome. palindromes: "hih", "i", "bob", "tenet", "soos", "madam" . not palindromes: "join", "hat", "frat", "supper", "rhythm". The code is mixed up and contains extra blocks. Put the necessary blocks in the correct order.

  1. back = back - 1;
  2. bool ispalindrome(string input) {
  3. else { #distractor
  4. front = front + 1;
  5. if( input[b] != input[e] ) {
  6. int front = 0 , back = input.length() - 1;
  7. int front = 0 , back = input.length();
  8. return false;
  9. return true;
  10. string ispalindrome(bool input) {
  11. while ( front &gt back) {
  12. while ( front &lt back) {
  13. }
  14. }
  15. }