Answer to Question #297384 in C++ for Rival

Question #297384

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.


1
Expert's answer
2022-02-13T17:12:03-0500
#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";
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog