Hi there!! this was the homework instructions: Palindrome Exercise - C++ You wil
ID: 3796417 • Letter: H
Question
Hi there!!
this was the homework instructions:
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:
mom
Yankees are number 1
A Santa at NASA.
Did Hannah see bees? Hannah did!
I used the 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. but I'm having lots of error and have no clue.... here is my code:// i don't know why this is not working...
#include <iostream>
#include <cstring>
#include <cctype>
#include "stdafx.h"
using namespace std;
void removeAllPunctuation(char str[])
{
//char *p;
char m[50] = "";
int len = strlen(str);
string word;
char *token;
int k = 0, i = 0;
token = strtok(str, " . ! ?");
while (token != '')
{ // iterating over characters of string
//cout << token << endl;
strcat(m, token);
token = strtok(NULL, " . ! ?");
// if current character character is digit or alphabetic then copy
}
//strcpy(str, len, m);
for (int i = 0; i < len; i++)
cout << m[i];
cout << endl;
}
bool isPalindrome(char str[])
{
char rev[50] = "";
int i = 0;
int j = strlen(str) - 1;
for (i = 0; i < strlen(str) - 1; i++)
{
rev[i] = str[j];
j--;
}
rev[i] = ''; // putitng null character at last
if (strcmp(str, rev) == 0)
return true;
else
return false;
}
int main()
{
char str[50] = "";
cout << "Enter string: " << endl;
cin.getline(str, sizeof(str), ' ');
//cout << str << endl;
removeAllPunctuation(str); // removing all punctuiations
//cout << str << endl;
if (isPalindrome(str))
cout << str << " Hurray!! This is a palindrome" << endl;
else
cout << str << " Sorry this is not a palindrome" << endl;
return 0;
}
Explanation / Answer
Program:
#include <iostream>
#include <cstring>
#include <cctype>
//#include "stdafx.h"
using namespace std;
void removeAllPunctuation(char str[])
{
//char *p;
char m[50] = "";
int len = strlen(str);
string word;
char *token;
int k = 0, i = 0;
token = strtok(str, " . ! ?");
while (token != '')
{ // iterating over characters of string
//cout << token << endl;
strcat(m, token);
token = strtok(NULL, " . ! ?");
// if current character character is digit or alphabetic then copy
}
//strcpy(str, len, m);
for (int i = 0; i < len; i++)
cout << m[i];
cout << endl;
}
bool isPalindrome(char str[])
{
char rev[50] = "";
int i = 0;
int j = strlen(str) - 1;
for (i = 0; i < strlen(str) - 1; i++)
{
rev[i] = str[j];
j--;
}
rev[i] = ''; // putitng null character at last
if (strcmp(str, rev) == 0)
return false;
else
return true;
}
int main()
{
char str[50] = "";
cout << "Enter string: " << endl;
cin.getline(str, sizeof(str), ' ');
//cout << str << endl;
removeAllPunctuation(str); // removing all punctuiations
//cout << str << endl;
if (isPalindrome(str))
cout << str << " Hurray!! This is a palindrome" << endl;
else
cout << str << " Sorry this is not a palindrome" << endl;
return 0;
}
Output:
Enter string: Did Hannah see bees? Hannah did!
DidHannahseebeesHannahdid
Did
Hurray!! This is a palindrome
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.