I'm making a program to find the larges palindrome in C++ and I need to get the palindromes for the inputted strings ignoring the case, punctuation and whitespace. For example, see the following line:
Confusius say: Madam, I'm Adam.
Here, the largest palindrome is Madam, I'm Adam if you ignore case, punctuation and whitespace.
The program also have to be efficient so as to test strings with 2000 characters in < 1 second. So I have the following code for returning the largest palindrome:
string largestPal(string input_str) {
string isPal = "";
string largest = "";
int j, k;
for(int i = 0; i < (input_str.length() - 1); ++i) {
k = i + 1;
j = i - 1;
if(j >= 0 && k < (input_str.length())) {
if(input_str[i] == input_str[j])
j--;
else if(input_str[i] == input_str[j])
k++;
}
while(j >= 0 && k < (input_str.length())) {
if(input_str[j] != input_str[k])
break;
else {
j--;
k++;
}
isPal = input_str.substr(j + 1, k - j - 1);
if(isPal.length() > largest.length()) {
largest = isPal;
}
}
}
return largest;
}
And I tried entering a fully-formatted string (without whitespace, punctuateion and case) as the parameter to this method and have succeeded in getting the output I want. (for example, the previous example returns MADAMIMADAM as the largest palindrome.
The question:
How can I convert this string back to the way it was (with punctuation, whitespace and case)?
OR
How can I test the stripped string directly within the method largestPal
, but return the original string (unstrippped) that corresponds to the selected largest palindrome?
Any help greatly appreciated!