Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <array>
- using namespace std;
- //Exercise 7.28
- string noSpaces(string str)
- {
- int len = str.length();
- for(int i{0}; i < len; ++i)
- {
- if(str[i] == ' ')
- {
- str.erase(i,1);
- }
- }
- return str;
- }
- bool testPalindrome(string str)
- {
- bool result{false};
- str = noSpaces(str);
- if(str.length() == 1)
- {
- result = true;
- }
- else if(str.length() == 2)
- {
- result = false;
- }
- else
- {
- if(str[0] == str[str.length() - 1]) //if the first and last char are the same
- {
- string newString{str.substr(1, str.length() - 2)};
- result = testPalindrome(newString);
- }
- else
- {
- result = false;
- }
- }
- return result;
- }
- //Exercise 7.31
- //Not sure how to do this with a void function, will probably come back to it.
- string stringReverse(string word, size_t subscript = 0)
- {
- string initialString{word.substr(subscript, word.length())};
- string reverseString{};
- if(initialString.length() == 1)
- {
- reverseString = initialString;
- }
- else
- {
- size_t stringSize{initialString.length()};
- string newString{initialString.substr(subscript,stringSize - 1)};
- reverseString = (initialString[stringSize - 1]) + stringReverse(newString, subscript);
- }
- return reverseString;
- }
- int main() {
- //Exercise 7.28
- cout << testPalindrome("a man a plan a canal panama") << endl;
- cout << testPalindrome("race car") << endl;
- cout << testPalindrome("not a palindrome") << endl;
- //Exercise 7.31
- cout << stringReverse("wyatt");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement