Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.74 KB | None | 0 0
  1. /**
  2.  *  @author Huy
  3.  *  @date 09.23.2018
  4.  *  @file h05.cpp
  5.  */
  6. #include <string>
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. string STUDENT = "hnguyen771"; // Add your Canvas/occ-email ID
  11.  
  12. #include "h05.h"
  13.  
  14. // Place your function definitions in this file.
  15. /**
  16.     zipZap(str) removes the middle letters from all "zip" or "zap" strings.
  17.  
  18.     @param str the input string.
  19.     @return Look for patterns like "zip" and "zap" in the input string:
  20.         any substring of length 3 that starts with "z" and ends with "p".
  21.         Return a string where for all such words, the middle letter
  22.         is gone, so "zipXzap" returns "zpXzp".
  23.         <ul>
  24.         <li>zipZap("zipXzap") returns "zpXzp"
  25.         <li>zipZap("zopzop") returns "zpzp"
  26.         <li>zipZap("zzzopzop") returns "zzzpzp"
  27.         </ul>
  28. */
  29. string zipZap(const string& str)
  30. {
  31.     string result;
  32.     // 1. Save the length (len) and create an index (i = 0)
  33.     // 2. If len is less than 3, then we just return the input str
  34.     // 3. Write a while loop that goes while i is less than len – 2
  35.     // 4. Inside the loop, extract a substring of 3 (word)
  36.     // 5. If word starts with 'z' and ends with 'p' then:
  37.     //      - add "zp" to the output string
  38.     //      - move forward three characters (to skip the "ip")
  39.     // 6. Otherwise
  40.     //      - add the first character of word to the output string
  41.     //      - move forward one character
  42.     // 7. After the loop, add the remaining characters, to the output
  43.  
  44.     size_t i;
  45.     size_t len = str.size();
  46.     i = 0;
  47.  
  48.     if (len < 3)
  49.     {
  50.         return str;
  51.     }
  52.  
  53.     while (i < len - 2)
  54.     {
  55.         string word = str.substr(i, 3);
  56.         if (word.front() == 'z' && word.back() == 'p')
  57.         {
  58.             result = result + "zp";
  59.             i = i + 3;
  60.         }
  61.         else
  62.         {
  63.             result = result + word.at(i);
  64.             i = i + 1;
  65.         }
  66.     }
  67.  
  68.  
  69.     return result;
  70. }
  71. /**
  72.     countCode(str) counts all occurences of the "code" pattern in str.
  73.  
  74.     @param str the input string.
  75.     @return the number of times that the string "code" appears
  76.         anywhere in the given input string, except that we'll accept
  77.         <b>any</b> letter for the 'd', so "cope" and both "cooe" count.
  78.         <ul>
  79.         <li>countCode("aaacodebbb") returns 1
  80.         <li>countCode("codexxcode") returns 2
  81.         <li>countCode("cozexxcope") returns 2
  82.         </ul>
  83. */
  84.  
  85. int countCode(const string& str)
  86. {
  87.     int result = 0;
  88.  
  89.     // Loop through str, grabbing a 4-character substring each time
  90.     size_t i;
  91.     size_t len = str.size();
  92.     for (i = 4; i <= len; i++)
  93.     {
  94.         string word = str.substr(i - 4, 4); // 4 or fewer characters
  95.         string front = word.substr(0, 2);
  96.         string back = word.substr(3);
  97.         if (front == "co" && back == "e")
  98.         {
  99.             result = result + 1;
  100.         }
  101.     }
  102.  
  103.  
  104.     return result;
  105. }
  106.  
  107. /**
  108.     everyNth(str, n) calculates every nth character.
  109.  
  110.     @param str the input string to check.
  111.     @param n the n-th character to use
  112.     @return the string made starting with char 0, and then
  113.         every n-th char of the string. So, if n is 3,
  114.         use char 0, 3, 6, and so on. If n is
  115.         less than 1, return the empty string
  116.         <ul>
  117.         <li>everyNth("Miracle", 2) returns "Mrce"
  118.         <li>everyNth("abcdefg", 2) returns "aceg"
  119.         <li>everyNth("abcdefg", 3) returns "adg"
  120.         </ul>
  121. */
  122. string everyNth(const string& str, int n)
  123. {
  124.     string result;
  125.     size_t i;
  126.     size_t len = str.size();
  127.  
  128.     for (i = 0; i < len; i = i + n)
  129.     {
  130.         result = result + str.at(i);
  131.     }
  132.  
  133.     return result;
  134. }
  135. /**
  136.     prefixAgain(str, n) returns true when the prefix(0,n) appears again in the string.
  137.  
  138.     @param str the input string.
  139.     @param n the number of characters to count for the prefix.
  140.     @return consider the prefix string made of the first n characters of
  141.         the input string. Does that prefix string appear somewhere else
  142.         in the string? Assume that the string is not empty and that n
  143.         is in the range 1..inStr.length().
  144.         <ul>
  145.         <li>prefixAgain("abXYabc", 1) returns true
  146.         <li>prefixAgain("abXYabc", 2) returns true
  147.         <li>prefixAgain("abXYabc", 3) returns false
  148.         </ul>
  149. */
  150. bool prefixAgain(const string& str, int n)
  151. {
  152.     // 1. Extract the first n characters into prefix
  153.     // 2. Start looping at 1, extracting n characters into word
  154.     //      - if word is equal to prefix, return true
  155.     // 3. If we get through the loop, return false
  156.     return str.substr(1).find(str.substr(0, n)) != string::npos;
  157. }
  158.  
  159.  
  160.  
  161.  
  162.  
  163. /////////// STUDENT TESTING //////////////////////
  164. int run()
  165. {
  166.     cout << "Student tests" << endl;
  167.     return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement