Question

In: Computer Science

I can’t get my code to work and/or finish it. Please fix. Below are code instructions...

I can’t get my code to work and/or finish it. Please fix. Below are code instructions and then sample runs and lastly my code so far.
//*********************************************************************
Program
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 whether to ignore 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
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 the terms that modifies how a 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 immediately after the program name and before any strings are processed and apply to all subsequent strings processed.
Flags must start with a minus (-) sign followed by flag values that can be capital or lowercase. e.g. -c, -S, -Cs, -Sc, -alphabetsoup, 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.
Any flag values beside c and s are invalid (see program flow notes below)
Options can appear in different flags, e.g. you can use -Sc or -S -c
Repeated flags should be ignored, e.g. -ccs
The argument -- (two dashes) signifies that every argument that follows is not a flag (allowing for strings that begin with a dash), e.g. palindrome -- -s

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. (Not a c string)
Return: void.
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 ignores spaces when true. (Not a c string)
Return: bool.
Calls helper function isPalindromeR to determine whether string is a palindrome.
String passed into isPalindromeR after dealing with flags.
If case insensitive, make all lower or upper case so that case does not matter.
If spaces are ignored, remove spaces from string.
Helper recursive function that determines whether a string is a character-unit palindrome. This does not deal with flags.
Name: isPalindromeR
Parameter(s): an input string (Not a c string)
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.
E.g. an invalid flag

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.)
Example Output
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.

#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
}

bool isPalindrome(string str, bool caps, bool space) {

//determines whether a string is a character-unit palindrome
//should do everytyhing to find palindrome
return false;
}

bool isPalindromeR(string str, start, end) {
if (start >= end)
return true;

if(str.at(start) != str.at(end))
return false;
return isPalindromeR(str, ++start, --end);

//helper recursive function that determines whether string is a character-unit palindrome
}


string tolower(string str) {
for (unsigned int i = 0; i < str.length(); i++){
str.at(i) = tolower(str.at(i));
}
return str; //change to return string in all lowercase chars
}

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

return str;
//change to return string w/ punctuation/spaces removed
}
//****
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 = true;
}
else { //not a flag
printUsageInfo(argv[0]);
break;
}
i++;
}//while
//cout << space << endl;
//cout << caps << endl;
} //if

else {

}

//TO DO

for(int j = startIndex; j < argc; ++j) {
if (caps) {
cout << tolower(argv[j]);
}
else if (space) {
cout << removePunctuation(argv[j], space);
}
else if (caps && space){
cout << "seriously, fix me";
}
else {
cout << "else";
}
}
cout << endl;

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


Solutions

Expert Solution

Given below is the fixed code for the question.
Please do rate the answer if it was helpful. Thank you

#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; //change to return string in all lowercase chars
}

string removePunctuation (string str, bool space) {
//change to return string w/ punctuation/spaces removed

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

bool isPalindromeR(string str, int start, int end) {
if (start >= end)
return true;

if(str.at(start) != str.at(end))
return false;
return isPalindromeR(str, ++start, --end);

//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, 0, str.length() - 1);
}


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

bool caps = false;
bool space = false;
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 = true;
}
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;
}
return 0;
}


Related Solutions

Python programming: can someone please fix my code to get it to work correctly? The program...
Python programming: can someone please fix my code to get it to work correctly? The program should print "car already started" if you try to start the car twice. And, should print "Car is already stopped" if you try to stop the car twice. Please add comments to explain why my code isn't working. Thanks! # Program goals: # To simulate a car game. Focus is to build the engine for this game. # When we run the program, it...
Please finish this code and make it work. This is my homework and my professor wont...
Please finish this code and make it work. This is my homework and my professor wont allow me to change the code in main, it's a set of huge numbers need to sort by radixsort with bit operation. #include <iostream> using namespace std; void radixLSD_help(int *items, int length, int bit) {    // – Count number of items for each bucket.    // – Figure out where each bucket should be stored (positions // of the first and last element...
I have to code the assignment below. I cannot get the program to work and I...
I have to code the assignment below. I cannot get the program to work and I am not sure what i am missing to get the code to work past the input of the two numbers from the user. My code is listed under the assignment details. Please help! Write a Java program that displays the prime numbers between A and B. Inputs: Prompt the user for the values A and B, which should be integers with B greater than...
please correct the error and fix this code: (i need this work and present 3 graphs):...
please correct the error and fix this code: (i need this work and present 3 graphs): Sampling_Rate = 0.00004; % which means one data point every 0.001 sec Total_Time = 0:Sampling_Rate:1; % An array for time from 0 to 1 sec with 0.01 sec increment Omega = 49.11; % in [rad/s] zeta=0.0542; %unitless Omega_d=49.03; % in [rad/s] Displacement_Amplitude = 6.009; % in [mm] Phase_Angle = 1.52; % in [rad] Total_No_of_Points = length(Total_Time); % equal to the number of points in...
I Have posted my Java code below. Fix the toString, add, and remove implementations so that...
I Have posted my Java code below. Fix the toString, add, and remove implementations so that the following test cases work. Note: I have removed all the unnecessary inherited List implementations. I have them to: throw new UnsupportedException(); For compilation, you could also add //TODO. Test (Main) List list = new SparseList<>(); list.add("0"); list.add("1"); list.add(4, "4"); will result in the following list of size 5: [0, 1, null, null, 4]. list.add(3, "Three"); will result in the following list of size...
I get errors in my merge method and Im not sure how to fix it to...
I get errors in my merge method and Im not sure how to fix it to make the code work public class OrderedApp1 { public static void main(String[] args) {    int maxSize = 100; // array size int searchKey = 55; OrdArray1 arr, a1, a2, a3; // reference to arrays arr = new OrdArray1(maxSize); // create the arrays a1 = new OrdArray1(maxSize); a2 = new OrdArray1(maxSize); a3 = new OrdArray1(maxSize);    //int a3[] =new int [ a1.length + a2.length];...
In Python I have a code: here's my problem, and below it is my code. Below...
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist. Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same. Examples: swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER' swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I...
In python I have my code written and I want just 4 functions to be fix...
In python I have my code written and I want just 4 functions to be fix in my code according to rule. My code is running but it has some problem regarding to rules. (I have did all the other things so you do not have to worry about other functions) 1) all the players has to play until he/she reaches to at least 500 points in first round. When user reach 500 points for the first time, user may...
I have to finish the code given by my diff eq professor to analyze the lorenz...
I have to finish the code given by my diff eq professor to analyze the lorenz function at different initial conditions and different values for p. I am not sure how to input the lorenz function in the way the code is set up. Here is the code provided for matlab: function lorenz s = 10; b = 8/3; p = 0.15; y = @(t,x) [ ; ; ]; % you are responsible for entering the lorenz system here T...
JAVA: How do I fix the last "if" statement in my code so that outputs the...
JAVA: How do I fix the last "if" statement in my code so that outputs the SECOND HIGHEST/MAXIMUM GPA out of the given classes? public class app { private static Object minStudent; private static Object maxStudent; public static void main(String args[ ]) { student st1 = new student("Rebecca", "Collins", 22, 3.3); student st2 = new student("Alex", "White", 19, 2.8); student st3 = new student("Jordan", "Anderson", 22, 3.1); student[] studentArray; studentArray = new student[3]; studentArray[0] = st1; studentArray[1] = st2; studentArray[2]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT