int main()
{
clrscr();
char c[80],d[80];
cout<<"Enter a string = ";
cin.get(a,80);
strcpy(c,a);
strrev(a);
if(strcmp(c,a)==0)
cout<<"String = "<<c<< "is palindrome.";
else
cout<<c<<" is not palindrome";
getch();
return 0;
}
so there is any other way to accomplish this task in easy way without using array or in other way?
#include <string>
#include <algorithm>
#include <iostream>
int main()
{
std::string s;
getline(std::cin, s);
if (std::equal(s.begin(), s.end(), s.rbegin()))
std::cout << s << " is a palindrome\n";
else
std::cout << s << " is not a palindrome\n";
}
No arrays, no pointers.
bool is_palindrome(const char* s)
{
const char *p = s;
const char *q = s + strlen(s) - 1;
while (p < q) {
if (*p != *q)
return false;
++p;
--q;
}
return true;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With