Palindrone (

the question is write a C++ string function that takes a string and returns true if It is a palindrome. Improve the function to make it have an option to make it case sensitive and adjustments to capital letters, punctuation, and word dividers. Examples "A man, a plan, a canal, Panama!", "race car".

The code runs perfectly but i tried to make it case sensitive but i seems to not work well. i also used getline to take the string as a whole but that also does not seem to work.

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

void trim(string& x) {
int k = 0;
while (k<x.size() && (x[k] == ' ' || x[k] == '\t' || x[k] == '\n' || x[k] == ',' || x[k] == '!' || x[k] == '?' || x[k] == '""'))
k++;
x.erase(0, k);

int s = x.size();
while (s>0 && (x[s - 1] == ' ' || x[s - 1] == '\t' || x[s - 1] == '\n' || x[s - 1] == '?' || x[s - 1] == '!' || x[s - 1] == '""'))
s--;
x.erase(s);
}

bool isPalindrome(const string &x)
{
if (equal(x.begin(), x.begin() + x.size() / 2, x.rbegin()))
{
return true;
}
return false;
}

int main()
{
string x;

cout << "Enter a string: ";
getline(cin,x);

cout << x;
cout << endl;
trim(x);
cout << x;
cout << endl;
cout << isPalindrome(x);
cout << endl;


system("pause");
return 0;
}
It seems to me that this code is already case sensitive. To make it caseINsensitive you may want to search for toUpper and toLower.
Last edited on
Thanx for the reply bu no its not case sensitive. I tried upper to lower but dont know how to implement it here.
Last edited on
Topic archived. No new replies allowed.