Question: Help with C++ Source Code: Submit the source code (main.cpp , functions.h , and functions.cpp files) Three Separate Files Program You will write a program

Help with C++ Source Code: Submit the source code (main.cpp , functions.h , and functions.cpp files) Three Separate Files

Program

You will write a program that uses a recursive function to determine whether a term is a character-unit palindrome. Moreover, flags can be used to indicate whether to do case sensitive comparisons or to stop 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 terms "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

Command Line Parameters

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

Terms

Each term will be separated by a space on the command line. If you want to include a term 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 term that is read in.

Flags

Optional for the user (but your program has to handle them!)

If present, flags always appear after the program name and before any terms are processed and apply to all subsequent terms 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).

Flag values are case insensitive.

c or C: Indicates that comparisons should be case-sensitive for all input terms. 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 terms. 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

Output printUsageInfo when you get an invalid flag, i.e. any value other s or c after the first dash (-)

Code Expectations

Your program should only get user input from the command line. (i.e. "cin" should not be anywhere in your code). Expect that a term will not be larger than 500 characters. You do not have to test for this

Required Functions:

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

Name: printUsageInfo

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

Return: void.

Function that determines whether a cstring is a character-unit palindrome.

Name: isPalindrome

Parameter(s): an input cstring, a boolean flag that considers case-sensitivity when true, and a boolean flag that ignores spaces when true.

Return: bool.

Calls helper function isPalindromeR to determine whether a cstring is a palindrome.

cString 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 cstring.

Helper recursive function that determines whether a cstring is a character-unit palindrome. This does not deal with flags. So it will be case sensitive and include spaces as characters.

Name: isPalindromeR

Parameter(s): an input cstring

Return: bool

Hint: Compare first and last characters, then send the substring composed of the cstring minus the first and last letters to the recursive call if needed. Here is a program where I get a substring based on user input.

Example: removeVowels.cpp

All functions should be placed in a separate file called functions.cpp with a header file called functions.h following the code organization conventions we covered.

Program Flow

1. Your program will take arguments from the command-line.

2. Determine if you have enough arguments. If not, output a usage message and exit program normally (return 0 or exit with code 0).

3. If flags are present.

1. Process and set values to use when processing a palindrome.

4. Loop through remaining arguments which are all input terms:

1. Process each by calling isPalindrome function with flag values.

2. 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. "preprocessCString" tolower - convert each letter to lowercase version for case insensitive comparisons. Youve already done this in a labwork.

Parameter(s): a cstring to be converted to lowercase removePunctuation - Remove all punctuation marks possibly including spaces depending on the flag value.

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

Hint: I have written a program that removes vowels from words that might help you figure out how to remove characters from a cstring. Example: substring.cpp

Existing functions that might help: tolower isalnum isspace

Example Output Note: you will probably only see the ./ if running in a unix environment.

/palindrome

Usage: ./palindrome [-c] [-s] ...

-c: turn on case sensitivity

-s: turn off ignoring spaces

./palindrome -c

Usage: ./palindrome [-c] [-s] ...

-c: turn on case sensitivity

-s: turn off ignoring spaces

./palindrome Kayak

"Kayak" is a palindrome

./palindrome Ka:yak?

"Ka:yak?" 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 "T@est Se*t"

"T@est Se*t" 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 -cd Usage:

Usage: ./palindrome [-c] [-s] ...

-c: turn on case sensitivity

-s: turn off ignoring spaces

./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.

./palindrome -s Kayak madam "Test Set" "Evil Olive" "test set" "loop pool"

"Kayak" is a palindrome.

"madam" is a palindrome.

"Test Set" is not a palindrome.

"Evil Olive" is not a palindrome.

"test set" is not a palindrome.

"loop pool" is a palindrome.

./palindrome -c Kayak madam "Test Set" "Evil Olive" "test set" "loop pool"

"Kayak" is not a palindrome.

"madam" is a palindrome.

"Test Set" is not a palindrome.

"Evil Olive" is not a palindrome.

"test set" is a palindrome.

"loop pool" is a palindrome.

./palindrome -c -s Kayak madam "Test Set" "Evil Olive" "test set" "loop pool"

"Kayak" is not a palindrome.

"madam" is a palindrome.

"Test Set" is not a palindrome.

"Evil Olive" is not a palindrome.

"test set" is not a palindrome.

"loop pool" is a palindrome.

I got up to this far, edit and add on to this. Thanks

functions.cpp

#include "functions.h"

#include

#include

#include

using namespace std;

bool isPalindrome(string str, bool C, bool S) {

int len = str.length();

string first = str.substr(0, 1);

string last = str.substr((len - 1), 1);

if (first == last) {

str = str.substr((0 + 1), (len - 2));

if (len <= 2) {

cout << "\"" << str << "\"" << " is a palindrome." << endl;

return true;

}

else {

isPalindrome(str, C, S);

}

}

else {

cout << "\"" << str << "\"" << " is not a palindrome." << endl;

return false;

}

}

void toLower(string& str) {

for (int i = 0; i < str.length(); ++i) {

if (isalpha(str[i])) {

if (isupper(str[i])) {

str[i] = str[i] + 32;

}

}

}

}

void removePunctuation(string& str) {

for (int i = 0; i < str.length(); ++i) {

if (ispunct(str[i]) || isspace(str[i])) {

str = str.erase(i, 1);

i = i - 1;

}

}

}

void printUsageInfo() {

cout << "Usage: ./palindrome [-c] [-s] ..." << endl;

cout << " -c: turn on sensitivity" << endl;

cout << " -s: turn off ignoring spaces";

}

functions.h

#ifndef FUNCTIONS_H #define FUNCTIONS_H #include #include using namespace std;

bool isPalindrome(string, bool, bool);

void toLower(string&);

void removePunctuation(string& str);

void printUsageInfo();

#endif

main.cpp

#include "functions.h"

#include

#include

#include

using namespace std;

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

bool C = false;

bool S = false;

string word;

string str;

string strf;

for (int i = 1; i < argc; ++i) {

str = argv[i];

strf = str.at(0);

if (strf == "-") {

if (str.find('C') != string::npos || str.find('c') != string::npos) {

C = true;

}

if (str.find('S') != string::npos || str.find('s') != string::npos) {

S = true;

}

if (i == (argc - 1)) {

printUsageInfo();

}

}

else {

word = argv[i];

if (C == false) {

toLower(str);

}

if (S == false) {

removePunctuation(str);

}

isPalindrome(str, C, S);

}

}

if (argc == 1) {

printUsageInfo();

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!