This is what I need to know to finish a program in c++. I want to be able to kno
ID: 3548601 • Letter: T
Question
This is what I need to know to finish a program in c++. I want to be able to know if a string is palindrome. Directions are here:
in main:
user will input c-string into char-array and send to user-defined functions
the c-string will be sent to a function:
- will remove all spaces and punctuation marks
- change any uppercase to lowercase
- use string tokens to eliminate spaces and punctuation marks
- include /0 NULL as concatenate the tokens int oa c-string
- copy back to the original c-string for the pass by reference
- Back in main, send c-string to a function that will see if it is really a palindrome
- create another c-string (contains the /0) that is filled up as the reverse of the argument c-string.
-check to see if the c-string is a palindrome
- return the boolean result
Back in main:
print the c-string (in all lowercase and without spaces and punctuations) and tell whether it is a palindrome or not.
Explanation / Answer
#include<iostream>
#include<cstring>
using namespace std;
void change_string(char str[])
{
int length = strlen(str);
for(int i=0; i<length; i++)
{
if(str[i]==' ' || str[i] == '!')
{
// remove space or punctuation mark and shift all to left.
for(int k=i; k<length; k++)
str[k] = str[k+1];
length--;
}
if(str[i]>=65 && str[i]<=90)
{
str[i] = str[i]+32;
}
}
str[length+1]='';
}
bool is_string_palindrome(char str[])
{
char newstring[strlen(str)+1];
for(int i=0; i<=strlen(str); i++)
{
newstring[i] = str[strlen(str)-i-1];
}
if(strcmp(str,newstring)==0)
return true;
return false;
}
int main()
{
char str[] = "MadaM";
char str2[] = "Ana GY YG anA!";
char str3[] = "Ashok Express";
change_string(str);
change_string(str2);
change_string(str3);
cout << "is "<< str << " is palindrome ?" << (is_string_palindrome(str)? " Yes":" No") << endl;
cout << "is "<< str2 << " is palindrome ?" << (is_string_palindrome(str2)? " Yes":" No") << endl;
cout << "is "<< str3 << " is palindrome ?" << (is_string_palindrome(str3)? " Yes":" No") << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.