Question

***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName)...

***************Fix code recursive function

#include <iostream>
#include <cctype>
#include <string>

using namespace std;

void printUsageInfo(string executableName) {
cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl;
cout << " -c: turn on case sensitivity" << endl;
cout << " -s: turn off ignoring spaces" << endl;
exit(1);

//prints program usage message in case no strings were found at command line
}

string tolower(string str) {
for(unsigned int i = 0; i < str.length(); i++) {
str.at(i) = tolower(str.at(i));
}
return str;
}

string removePunctuation(string str, bool space) {
for(unsigned int i = 0; i < str.length(); i++) {
if ((str.at(i) == ' ' && space) || ispunct(str.at(i))) {
str.erase(i,1);
}
}
return str;
}


//function takes only string parameter and recurses through string to get sample out listed below code
bool isPalindromeR(string str) {
#FIXME
int start = 0;
int end = str.length() - 1;

if (start >= end)
return true;

if(str.at(start) != str.at(end))
return false;
if (start < end)
++start;
--end;
return isPalindromeR(str);
//helper recursive function that determines whether string is a character-unit palindrome
}


bool isPalindrome(string str, bool caps, bool space) {
//determines whether a string is a character-unit palindrome
//should do everytyhing to find palindrome

if(!caps)
str = tolower(str);
str = removePunctuation(str, space);

return isPalindromeR(str);
}
int main(int argc, char* argv[]) {

bool caps = false;
bool space = true;
string executableName = argv[0];


if (argc < 2)
printUsageInfo(argv[0]);

int startIndex = 1;
int i = 1;

if ('-' == argv[1][0]) {
startIndex++;
while((argv[1][i]) != '\0') {
if ('c' == tolower(argv[1][i])){
caps = true;
}
else if ('s' == tolower(argv[1][i])) {
space = false;
}
else { //not a flag
printUsageInfo(argv[0]);
break;
}
i++;
}//while
//cout << space << endl;
//cout << caps << endl;
} //if

if(startIndex == argc)
printUsageInfo(argv[0]);


else {
for(int j = startIndex; j < argc; ++j) {
if(isPalindrome(argv[j], caps, space))
cout << "\"" << argv[j] << "\" is a palindrome." << endl;
else
cout << "\"" << argv[j] << "\" is not a palindrome." << endl;
}
cout << endl;

}
//Test
//isPalindrome(cup, bool caps, bool space);
//cout << "end of program" << endl;
return 0;
}



Example Output
Note: the ./ is used in the terminal to indicate that the file is in the current directory.
Assumes executable is named palindrome
$ g++ … -o palindrome …
./palindrome
Usage: ./palindrome [-c] [-s] string ...
-c: turn on case sensitivity
-s: turn off ignoring spaces

./palindrome -c
Usage: ./palindrome [-c] [-s] string ...
-c: turn on case sensitivity
-s: turn off ignoring spaces

./palindrome Kayak
"Kayak" is a palindrome.

./palindrome -c Kayak
"Kayak" is not a palindrome.

./palindrome -C Kayak
"Kayak" is not a palindrome.

./palindrome -c kayak
"kayak" is a palindrome.

./palindrome "Test Set"
"Test Set" is a palindrome.

./palindrome -sc "Test Set"
"Test Set" is not a palindrome.

./palindrome -s -c "Test Set"
"Test Set" is not a palindrome.

./palindrome -s -s "Test Set"
"Test Set" is not a palindrome.

./palindrome -scs "Test Set"
"Test Set" is not a palindrome.

./palindrome Kayak madam "Test Set" "Evil Olive" "test set" "loop pool"
"Kayak" is a palindrome.
"madam" is a palindrome.
"Test Set" is a palindrome.
"Evil Olive" is a palindrome.
"test set" is a palindrome.
"loop pool" is a palindrome.



0 0
Add a comment Improve this question Transcribed image text
Answer #1

// For any doubt, feel free to comment.

I think that your bool isPalindromeR(string str) should be like this:-

bool isPalindromeR(string str) {
#FIXME
if(str.length()<=1) //Lines added
return true; // Lines added

int start = 0;
int end = str.length() - 1;

if(str.at(start) != str.at(end))
return false;

return isPalindromeR(str.substr(1,str.length()-2)); // Send substring by deleting first and last character.
//helper recursive function that determines whether string is a character-unit palindrome
}

When we use recursion then each function calls has its own local variables. So, your values of start and end will change in every recursive function call. So, to overcome this difficulty, we will send the string by deleting its first and last character by using str.substr() function. We delete the first and last character of current str because we have checked the palindrome condition for them and we don't have to check for it again.

And whenever str.length() is less than or equal to 1. Then str will be obviously palindrome so we return true.

If you have any further doubt, kindly ask in comments and I will try to reply asap.

Add a comment
Know the answer?
Add Answer to:
***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I keep getting an error code in my C++ program. It says "[Error] 'strlen' was not...

    I keep getting an error code in my C++ program. It says "[Error] 'strlen' was not declared in this scope" in my main.cpp. Here are my codes. main.cpp #include <iostream> #include <string> #include "functions.h" using namespace std; //definition of the main function. //takes arguments from the command-line. int main(int argc, char *argv[]) { //Determine if you have enough arguments. //If not, output a usage message and exit program if (argc<2 || (argc == 2 && argv[1][0] == '-')) { //call...

  • Please add a detailed comment for this program. #include<iostream> #include<string> #include<fstream> #include<sstream> #include<cctype> using namespace std;...

    Please add a detailed comment for this program. #include<iostream> #include<string> #include<fstream> #include<sstream> #include<cctype> using namespace std; int is_palindrome(string word){ int len = word.size(); for(int i=0; i<len/2; i++){ if(toupper(word[i])!=toupper(word[len-i-1])) return 0; } return 1; } int have_vowels3(string word){ int cnt = 0; for(int i=0; i<word.size(); i++){ if(tolower(word[i])=='a' || tolower(word[i])=='e' || tolower(word[i])=='i' || tolower(word[i]) =='o' || tolower(word[i]) == 'u') cnt++; } if(cnt>=3) return 1; else return 0; } int have_consecutives(string word){ for(int i=0; i<word.size()-1; i++){ if(tolower(word[i])=='o' && tolower(word[i+1]=='o')) return 1; } return...

  • Fix the function so that it checks if the string is a palindrome #include <iostream> using...

    Fix the function so that it checks if the string is a palindrome #include <iostream> using namespace std; //Fix the function so that it checks if the string is a palindrome //(same forwards as backwards ex: racecar / radar) bool is_palindrome(string str, int i){ //base cases if (/* add the condition */) return false;    if (/* add the condition */) return true;    //recursive call return is_palindrome(str, i-1); } int main(){ string x; cin >> x;    if (is_palindrome(x,x.length())){...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void...

    Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void messageAndKey(){ string msg; cout << "Enter message: "; getline(cin, msg); cin.ignore(); //message to uppercase for(int i = 0; i < msg.length(); i++){ msg[i] = toupper(msg[i]); } string key; cout << "Enter key: "; getline(cin, key); cin.ignore(); //key to uppercase for(int i = 0; i < key.length(); i++){ key[i] = toupper(key[i]); } //mapping key to message string keyMap = ""; for (int i = 0,j...

  • #include <iostream> #include <string> #include "hashT.h" #include "stateData.h" using namespace std; void stateData::setStateInfo(string sName, string sCapital,...

    #include <iostream> #include <string> #include "hashT.h" #include "stateData.h" using namespace std; void stateData::setStateInfo(string sName, string sCapital,    double stArea, int yAdm, int oAdm) {    stateName = sName; stateCapital = sCapital; stArea = stateArea; yAdm = yearOfAdmission; oAdm = orderOfAdmission;       } void stateData::getStateInfo(string& sName, string& sCapital,    double& stArea, int& yAdm, int& oAdm) {    sName = stateName; sCapital = stateCapital; stArea = stateArea; yAdm = yearOfAdmission; oAdm = orderOfAdmission;       } string stateData::getStateName() { return stateName;...

  • #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car {...

    #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...

  • PLEASE HELP WITH THE FIX ME'S #include #include #include #include "CSVparser.hpp" using namespace std; //==...

    PLEASE HELP WITH THE FIX ME'S #include #include #include #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() { amount = 0.0; } }; //============================================================================ // Linked-List class definition //============================================================================ /** * Define a class containing data members and methods to *...

  • Find Output. Just output. No explanation needed.. #include <iostream> #include <string> using namespace std; class baseClass...

    Find Output. Just output. No explanation needed.. #include <iostream> #include <string> using namespace std; class baseClass { public: void print() const; baseClass(string s = " ", int a = 0); //Postcondition: str = s; x = a; protected: int x; private: string str; }; class derivedClass: public baseClass { public: void print() const; derivedClass(string s = "", int a = 0, int b = 0); //Postcondition: str = s; x = a; y = b; private: int y; }; int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT