Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Huy
- * @date 09.23.2018
- * @file h05.cpp
- */
- #include <string>
- #include <iostream>
- using namespace std;
- string STUDENT = "hnguyen771"; // Add your Canvas/occ-email ID
- #include "h05.h"
- // Place your function definitions in this file.
- /**
- zipZap(str) removes the middle letters from all "zip" or "zap" strings.
- @param str the input string.
- @return Look for patterns like "zip" and "zap" in the input string:
- any substring of length 3 that starts with "z" and ends with "p".
- Return a string where for all such words, the middle letter
- is gone, so "zipXzap" returns "zpXzp".
- <ul>
- <li>zipZap("zipXzap") returns "zpXzp"
- <li>zipZap("zopzop") returns "zpzp"
- <li>zipZap("zzzopzop") returns "zzzpzp"
- </ul>
- */
- string zipZap(const string& str)
- {
- string result;
- // 1. Save the length (len) and create an index (i = 0)
- // 2. If len is less than 3, then we just return the input str
- // 3. Write a while loop that goes while i is less than len – 2
- // 4. Inside the loop, extract a substring of 3 (word)
- // 5. If word starts with 'z' and ends with 'p' then:
- // - add "zp" to the output string
- // - move forward three characters (to skip the "ip")
- // 6. Otherwise
- // - add the first character of word to the output string
- // - move forward one character
- // 7. After the loop, add the remaining characters, to the output
- size_t i;
- size_t len = str.size();
- i = 0;
- if (len < 3)
- {
- return str;
- }
- while (i < len - 2)
- {
- string word = str.substr(i, 3);
- if (word.front() == 'z' && word.back() == 'p')
- {
- result = result + "zp";
- i = i + 3;
- }
- else
- {
- result = result + word.at(i);
- i = i + 1;
- }
- }
- return result;
- }
- /**
- countCode(str) counts all occurences of the "code" pattern in str.
- @param str the input string.
- @return the number of times that the string "code" appears
- anywhere in the given input string, except that we'll accept
- <b>any</b> letter for the 'd', so "cope" and both "cooe" count.
- <ul>
- <li>countCode("aaacodebbb") returns 1
- <li>countCode("codexxcode") returns 2
- <li>countCode("cozexxcope") returns 2
- </ul>
- */
- int countCode(const string& str)
- {
- int result = 0;
- // Loop through str, grabbing a 4-character substring each time
- size_t i;
- size_t len = str.size();
- for (i = 4; i <= len; i++)
- {
- string word = str.substr(i - 4, 4); // 4 or fewer characters
- string front = word.substr(0, 2);
- string back = word.substr(3);
- if (front == "co" && back == "e")
- {
- result = result + 1;
- }
- }
- return result;
- }
- /**
- everyNth(str, n) calculates every nth character.
- @param str the input string to check.
- @param n the n-th character to use
- @return the string made starting with char 0, and then
- every n-th char of the string. So, if n is 3,
- use char 0, 3, 6, and so on. If n is
- less than 1, return the empty string
- <ul>
- <li>everyNth("Miracle", 2) returns "Mrce"
- <li>everyNth("abcdefg", 2) returns "aceg"
- <li>everyNth("abcdefg", 3) returns "adg"
- </ul>
- */
- string everyNth(const string& str, int n)
- {
- string result;
- size_t i;
- size_t len = str.size();
- for (i = 0; i < len; i = i + n)
- {
- result = result + str.at(i);
- }
- return result;
- }
- /**
- prefixAgain(str, n) returns true when the prefix(0,n) appears again in the string.
- @param str the input string.
- @param n the number of characters to count for the prefix.
- @return consider the prefix string made of the first n characters of
- the input string. Does that prefix string appear somewhere else
- in the string? Assume that the string is not empty and that n
- is in the range 1..inStr.length().
- <ul>
- <li>prefixAgain("abXYabc", 1) returns true
- <li>prefixAgain("abXYabc", 2) returns true
- <li>prefixAgain("abXYabc", 3) returns false
- </ul>
- */
- bool prefixAgain(const string& str, int n)
- {
- // 1. Extract the first n characters into prefix
- // 2. Start looping at 1, extracting n characters into word
- // - if word is equal to prefix, return true
- // 3. If we get through the loop, return false
- return str.substr(1).find(str.substr(0, n)) != string::npos;
- }
- /////////// STUDENT TESTING //////////////////////
- int run()
- {
- cout << "Student tests" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement