Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Palindrome Exercise - C++ You will create a program that will use c-string funct

ID: 673090 • Letter: P

Question

Palindrome Exercise - C++

You will create a program that will use c-string functions to determine if an inputted c-string is a palindrome or not. In main, the user will input a c-string into a character array and send to user-defined functions.

This c-string will be sent to a function that will remove all spaces and punctuation marks from its c-string as well as change any uppercase letters to lowercase. Use string tokens to eliminate the spaces & punctuation marks. Make sure you include the NULL as you concatenate the tokens into a c-string. Then copy back into the original c-string for the pass by reference.

Back in main, you will then send the c-string to a function that will see if it is really a palindrome or not. You will need to create another c-string (that contains the ) that is filled up as the reverse of the argument c-string. Then check to see if the c-string is a palindrome and return the boolean result.

Back in main, print the c-string (now in all lowercase and without the spaces and punctuation marks) and tell whether it is a palindrome or not.

Add sufficient documentation to your file name, exercise, at least a 4 line full paragraph about purpose, & at least 5 comments throughout explaining what is going on (what functions do or if a line of code's purpose is not obvious to a novice programmer what that statement does, etc.). Run the palindrome program with the following data, and check the output results (you can copy and paste them into a blank text file). Submit the .cpp and the .txt files.

Data for possible palindromes:

mom

Yankees are number 1

A Santa at NASA.

Did Hannah see bees? Hannah did!

Explanation / Answer

//The program reads an input string in main() and passes this string to another function
//called stringModifier() which will just remove all the spaces and punctuations from the
//given input string, and will store it into the same referred variable.
//The modified string is then passed to another function called palindromeChecker which
//will check whether this string is a palindrome or not. It will return true to main, if
//it is a palindrome, else it will return false, stating that it is not a palindrome.
//Finally, in the main, the string along with the assertion on palindrome will be printed on to the screen.
#include <iostream>
#include <cstring>
using namespace std;
void stringModifier(char input[100]) //Given the input string, it removes the spaces and punctuations, and will convert upper case alphabets to lower case.
{
char *x, output[100];
int i = 0;
x = input;
while(*x != '') //As long as there are elements in the string.
{
if(*x >= 65 && *x <= 90)   //If the character is a capital Alphabet.
output[i++] = *x + 32;       //Convert it into small letter and store it.
else if(*x >= 97 && *x <= 122)   //If the character is a small Alphabet.
output[i++] = *x;            //Just add it to the output.
x++;                        //Proceed to the next character.
}
output[i] = '';               //Just add at the end.
strcpy(input,output);        //Copy the newly created string into the input string.
}

int palindromeChecker(char input[100])   //Checks whether a given string is palindrome or not.
{
char *x;
char dup[100];
int i, j = 0;
x = input;                   //Assigning the input array starting address to x.
int len = strlen(input);       //Calculating the length of the input array.
while(*x != '')               //Move to the end of the input array.
x++;
for(i = len-1; i >= 0; i--)   //Copy one by element from the array to another array dup[], in reverse order.
dup[j++] = input[i];
dup[j] = '';                //Just add at the end.
int flag = strcmp(dup,input);   //If the original input and the reversed array dup are the same.
if(flag == 0)
return 1;                   //Return 1, which means it is a palindrome.
else
return 0;                    //If not return 0, which means it is not a palindrome.
}
int main()
{
char input[100];
cout<<"Enter the string: ";   //Reads the input string.
gets(input);                  
stringModifier(input);       //Calls the function, to remove spaces and punctuations.
int flag = palindromeChecker(input);   //Calls a function, to check whether it is a palindrome or not.
cout<<"The string you entered is: "<<input;
if(flag == 0)                           //If NOT palindrome.
cout<<" and is NOT a palindrome."<<endl;   //Display NO.
else                                   //If palindrome.
cout<<" and is a palindrome."<<endl;    //Display YES.
}