Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ help program something like this? Write a program to determine if the user\'

ID: 3860187 • Letter: C

Question

C++ help program something like this?

Write a program to determine if the user's input is a palindrome or not Recall that a palindrome is a word which is the same when spelled forward or backward: deed radar mom 0tto 2112 racecar (I didn't say they had to make sense...) The only trick to palindromes is remembering to ignore capitalization. The book's solution is nifty, but doesn't occur until Chapter 10. (I'll be that's a bit out of our league..! Although reading the first page or two couldn't hurt us to get ideas...) In another text (or was it on the InterWebs..?) we find them creating the reverse of the string and comparing this to the original. It may seem clever at first, but it isn't a good approach. Neither is the common part of the solution to make the string all lower-case before comparing. (Nor is removing all the punctuation/spacing by duplicating what isn't into a copy of the string when doing phrase-style palindromes...not that that would concern us. "shrug") All of these suffer from two problems: duplicate storage of essentially the same information when we aren't going to ever need it again and processing the user's string multiple times to decide if it is a palindrome or not. (Some suffer that last problem in spades!) You'll need to remove all these inefficiencies in your solution. You should only have to make one pass through the string to determine its palindromicity (is that even a word?? "shrug"). Ideally, your code for determining palindrom-ness would be encapsulated within a function - making the main little more than a nice driver. However, I suppose that could be an option. "sigh"

Explanation / Answer

#include <iostream>
#include<string>
using namespace std;

//function to check the string is palindrom or not
bool isPalindrome(string str)
{
int length = str.length();

for (int i = 0; i < length / 2; i++)
if (str[i] != str[length - 1 - i])
return false;

return true;
}

int main()
{
//declaration
string str;

//prompt to enter the string
cout << "Enter the string: ";
cin >> str;

//check the string
if(!isPalindrome(str))
cout << str << " is not a palindrome!" << endl;
else
cout << str << " is a palindrome!" << endl;
return 0;
}

OUTPUT:

Test 1:

Enter the string: 2332
2332 is a palindrome!

Test 2:
Enter the string: racecar
racecar is a palindrome!

Test 3:
Enter the string: mom
mom is a palindrome!

Test 4:
Enter the string: madhavi
madhavi is not a palindrome!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote