Write a program that accepts a string as an input and perform following –
Write a function that accepts a string as an argument and display the reverse of that string and return that string.
Write another function that accepts a string as its argument and check whether the string is palindrome or not.
#include <iostream>
std::string reverse(std::string str)
{
std::string res = "";
res.resize(str.size());
for (int i = str.size()-1; i >= 0; i--)
{
res[i] = str[str.size() - 1 - i];
}
return res;
}
bool is_palindrome(std::string str)
{
if (reverse(str) == str)
{
return 1;
}
return 0;
}
int main()
{
std::string str;
std::cout << "Enter string : ";
std::cin >> str;
std::cout << "Reverse string : " << reverse(str) << '\n';
if (is_palindrome(str))
{
std::cout <<str << " is palindrome\n";
}
else
{
std::cout << str << " is not palindrome\n";
}
std::cout << "Hello World!\n";
}
Comments
Leave a comment