Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will write a program that uses a recursive function to determine whether a string is a character-unit palindrome. Moreover, flags can be used to

You will write a program that uses a recursive function to determine whether a string is a character-unit palindrome. Moreover, flags can be used to indicate whether to do case sensitive comparisons and/or not ignoring spaces. For example "A nut for a jar of tuna" is a palindrome if spaces are ignored and not otherwise. "Step on no pets" is a palindrome whether spaces are ignored or not, but is not a palindrome if it is case sensitive since the S and s are not the same.

Background

Palindromes are character sequences that read the same forward or backwards (e.g. the strings "mom" or "123 454 321"). Punctuation and spaces are frequently ignored so that phrases like "Dog, as a devil deified, lived as a god." are palindromes. Conversely, even if spaces are not ignored phrases like "Rats live on no evil star" are still palindromes.

Specifications

Design

Create a flowchart or use pseudocode to indicate how the recursion will determine if a given string is a palindrome. Dont forget the rules for creating a good recursive function.

Create a flowchart of use pseudocode to indicate how to get values from the list of command line parameters and determine whether you have a flag and dealing with those as well.

Determine test cases that will never be a palindrome, always be a palindrome, as well as cases where its status as a palindrome depends on the flags.

Command Line Parameters

The program name will be followed by a list of strings. The program will determine whether each string is a palindrome and output the results. Punctuation will always be ignored. An optional flag can precede a term that modifies how the palindrome is determined.

Strings

Each string will be separated by a space on the command line. If you want to include a string that has spaces in it (e.g. "Rats live on no evil star"), then put quotes around it. The quote characters will not be part of the string that is read in.

Flags

Optional for the user

If present, flags always appear after the program name and before any strings are processed and apply to all subsequent strings processed.

Must start with a minus (-) sign followed by one or two flag values that can be capital or lowercase. e.g. -c, -S, -Cs, -Sc, etc.

There are no spaces between starting minus (-) sign and flag(s).

Flags values are case insensitive.

c or C: Indicates that comparisons should be case-sensitive for all input strings. The default condition (i.e. if the flag is NOT included) is to ignore case-sensitivity. So, for example:

palindrome Mom should evaluate as being a palindrome.

palindrome -c Mom should not evaluate as being a palindrome.

s or S: Indicates that comparisons should not ignore spaces for all input strings. The default condition (i.e. if the flag is NOT included) is to ignore spaces. So, for example:

palindrome "A nut for a jar of tuna" should evaluate as being a palindrome.

palindrome -s "A nut for a jar of tuna" should not evaluate as being a palindrome.

Options can appear in different flags, e.g. you can use -Sc or -S -c

Code Expectations

Your program should only get user input from the command line. (i.e. "cin" should not be anywhere in your code).

Required Functions:

Function that prints program usage message in case no input strings were found at command line.

Name: printUsageInfo

Parameter(s): a string representing the name of the executable from the command line.

Return: void.

Recursive function that determines whether a string is a character-unit palindrome.

Name: isPalindrome

Parameter(s): an input string, a boolean flag that considers case-sensitivity when true, and a boolean flag that does not ignore spaces when true.

Return: bool.

All functions should be placed in a separate file following the code organization conventions we covered.

Program Flow

Your program will take arguments from the command-line.

Determine if you have enough arguments. If not, output a usage message and exit program.

If flags are present.

Process and set values to use when processing a palindrome.

Loop through remaining arguments which are all input strings:

Process each by calling isPalindrome function with flag values.

Output results

Program Flow Notes:

Any time you encounter a syntax error, you should print a usage message and exit the program immediately.

Hints

Remember rules for a good recursive function.

Recommended Functions to write: Note: You could combine these into a single function. e.g. "preprocessString"

tolower - convert each letter to lowercase version for case insensitive comparisons.

Parameter(s): a string to be converted to lowercase

Return: a string with all lowercase characters

removePunctuation - Remove all punctuation marks possibly including spaces depending on the flag value.

Parameter(s): a string and a boolean flag indicating whether to also remove spaces

Return: a string with punctuation/spaces removed

Existing functions that might help:

tolower

substr

isalnum

erase (This can be used instead of substr, but is a bit more complicated.)

Development Environment:

It is easier to test a command line program on the command line. So I suggest that you test your program on compute.cse.tamu.edu.

These will be graded on vocareum which uses the same compiler as compute.cse.tamu.edu. So at a minimum, make sure it compiles and runs on compute.cse.tamu.edu before submitting, even if you develop it in your IDE.

Example Output

Note: you will probably only see the ./ if running on build.tamu.edu.

./palindrome Usage: ./palindrome [-c] [-s] string ... -c: case sensitivity turned on -s: ignoring spaces turned off ./palindrome -c Usage: ./palindrome [-c] [-s] string ... -c: case sensitivity turned on -s: ignoring spaces turned off ./palindrome Kayak "Kayak" is a palindrome ./palindrome -c Kayak "Kayak" is not a palindrome.

#include

#include

//#include

using namespace std;

bool isPalindrome(string Pal, int First, int Last);

int checkFlag(string cons);

string preprocessString(string Line, int constraint);

//./palindrome -s "Kayak"

int main(int argc, char* argv[])

{

string phrase1, phrase2, sentence;

int flag = 0, next = 1;

if(argc >= 3) {

phrase1 = argv[1];

phrase2 = argv[2];

flag = checkFlag(phrase2);

flag += checkFlag(phrase1);

if(flag == 1 || 2 || 4)

next++;

else if(flag == 3)

next += 2;

}

while(next <= argc){

sentence = argv[next];

sentence = preprocessString(sentence, flag);

if (isPalindrome(sentence, 0, sentence.size()) == true)

cout << sentence << " is a palindrome";

else

cout << sentence << " is not a palindrome";

next++;

}

}

int checkFlag(string cons) {

int num_flag = 0;

if(cons == "-s" || "-S")

num_flag = 1;

if(cons == "-c" || "-C")

num_flag = 2;

if(cons == "-SC" || "-Sc" || "-sC" || "-sc" || "-scs")

num_flag = 4;

return num_flag;

}

string preprocessString(string Line, int constraint) {

if(constraint == 1)

Line.replace(Line.begin(), Line.end()," ","");

else if(constraint == 2)

for(int i = 0; i < Line.size(); i++)

Line.at(i) = tolower(Line.at(i));

else if(constraint >= 3) {

Line.replace(Line.begin(), Line.end()," ","");

for(int i = 0; i < Line.size(); i++)

Line.at(i) = tolower(Line.at(i));

}

return Line;

}

bool isPalindrome(string Pal, int First, int Last){

if (First < Last){

if (Pal.at(First) == Pal.at(Last))

isPalindrome(Pal, ++First,--Last);

else

return false;

}

else {

return true;

}

}

My program currently does not execute any sample inputs like ./palindrome -s kayak

How do I fix this?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions