1. A palindrome is a string that reads the same forward and backward, for examples, redivider, deified, civic, radar, racecar, redder.
Write a short program( in C++) that is the most efficient you can think of for determining whether an input string of n characters is a palindrome.
#include <iostream> #include <string> using namespace std; bool isPalindrome(string); int main() { cout << "Enter a string: "; string s; cin >> s; if (isPalindrome(s)) { // check if it's a palindrome cout << s << " is a palindrome" << endl; } else { cout << s << " is not a palindrome" << endl; } return 0; } bool isPalindrome(string s) { for (int i = 0; i < s.length(); ++i) { // go through all characters if (s[i] != s[s.length() - i - 1]) { // if characters from beginning is not same as character from end return false; // then, return false } } return true; // if it's a palindrome then return true }
Get Answers For Free
Most questions answered within 1 hours.