Your program will take arguments from the command-line , including one or more i
ID: 3679717 • Letter: Y
Question
Your program will take arguments from the command-line, including one or more input strings to be evaluated with your recursive palindrome-checking function. An optional command-line flag (-c), if included in the command line, will tell your program to 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, "Mom" should evalate as a palindrome if the -c flag is not included in the command-line, but should evaluate as NOT a palindrome if -c is included.
Important note: Spaces normally separate command line arguments (in this case, the input strings), but a string with spaces can be passed to a program at the command line by putting it in double quotes (e.g. "this will be read by my program as a single string"). The quote characters are not included in the string that gets parsed.
Requirements
Assume that if the case-sensitivity flag (-c) is present, it is always the first parameter passed at the command line.
Allow both uppercase and lowercase versions of the flag (i.e. -c and -C should do the same thing).
Your program should not take any user input (i.e. "cin" should not be anywhere in your code).
You must have the following functions:printUsageInfo - Print program usage information in case no input strings were found at command line.
Parameter(s): a string (passed by constant reference) representing the name of the executable from the command line.
Return: void.
isPalindrome - This function determines whether a string is a character-unit palindrome.
Parameter(s): an input string (passed by constant reference), and a boolean flag that says whether to consider case-sensitivity.
Return: bool.
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
int main(int argc, char *argv[]){
char string1[20];
int i, length;
int flag = 0;
if(argc==3)
string1=*argv[2];
else
string1=*argv[1];
length = strlen(string1);
if(strcmp(*argv[1],"-C")==0)
{
for(i=0;i < length ;i++){
if(isupper(string1[i]) != isupper(string1[length-i-1]) && string1[i] != string1[length-i-1]){
flag = 1;
break;
}
}
}
else
{
for(i=0;i < length ;i++){
if(string1[i] != string1[length-i-1]){
flag = 1;
break;
}
}
}
if (flag) {
cout << string1 << " is not a palindrome" << endl;
}
else {
cout << string1 << " is a palindrome" << endl;
}
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.