In C++, write a program that accomplishes the following: Given a string of text,
ID: 3585806 • Letter: I
Question
In C++, write a program that accomplishes the following:
Given a string of text, determine whether it is a palindrome. A palindrome is text that consists of letters that would look the same whether you printed them in order or in reverse order. So, for example, RACECAR or HANNAH is a palindrome, but RACECAR RACECAR or HANNAH HANNAH is not.
(Edit: To clarify why RACECAR RACECAR is not a palindrome is because it is 2 words separated by a space. I believe it is supposed to just be one word. I hope this explain it sufficiently).
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
char arr[20];
int i, length;
int status = 0;
cout << "Enter a string: ";
cin.get( arr, sizeof(arr) ); /*if we not use get function the editor will not allow your print space*/
length = strlen(arr);
for(i=0;i < length ;i++)
{
if(arr[i]==' ') // is user given space as charcater then it will set status 1
{
status =1;
}
if(arr[i] != arr[length-i-1]) // comparing characters
{
status = 1;
break;
}
}
if (status) {
cout << arr << " is not a palindrome" << endl;
}
else {
cout << arr << " is a palindrome" << endl;
}
return 0;
}
output:
Enter a string: racecar racecar
racecar racecar is not a palindrome
According to your requirement if tehre is space between stringsthen it is not palindrome
************************************************************************************************************************
#include <iostream>
using namespace std;
int main()
{
char arr[20];
int i, length;
int status = 0;
cout << "Enter a string: ";
//cin.get( arr, sizeof(arr) ); /*if we not use get function the editor will not allow your print space*/
cin >>arr;
length = strlen(arr);
for(i=0;i < length ;i++)
{
if(arr[i]==' ') // is user given space as charcater then it will set status 1
{
status =1;
}
if(arr[i] != arr[length-i-1]) // comparing characters
{
status = 1;
break;
}
}
if (status) {
cout << arr << " is not a palindrome" << endl;
}
else {
cout << arr << " is a palindrome" << endl;
}
return 0;
}
output:
Enter a string: racecar
racecar is a palindrome