Removing Punctuation and Whitespaces

For this program I had to input a file from a .txt document and check if each line is a Palindrome or not. This program works how I would like it to work besides one thing. I have to remove all white space and punctuation from the file prior to checking if its a Palindrome or not. If anyone could show me how to do this I would be very appreciative because I have been trying for a while.





#include <iostream>
#include <fstream>
#include <string>

using namespace std;

bool isPalindrome(string, int);

int main()
{
string sentence;
bool Palindrome;
int len;

ifstream inData;
ofstream outData;
inData.open("InPalindrome.txt");
outData.open("OutPalindrome.txt");

if (!inData)
{
cout << "Can't open input file!";
getchar();
return 1;
}

if (!outData)
{
cout << "Can't open output file!";
getchar();
return 2;
}

getline(inData, sentence);
while(inData)
{
len = sentence.length();
len--;
Palindrome = isPalindrome(sentence, len);
cout << "Input Value: " << sentence << endl;
outData << "Input Value: " << sentence << endl;
cout << "Is it a palindrome? ";

outData << "Is it a palindrome? ";
if(Palindrome)
{
cout << "Yes \n" << endl;
outData << "Yes \n" << endl;
}

else
{
cout << "No \n" << endl;
outData << "No \n" << endl;
}
getline(inData, sentence);
}

getchar();
return 0;
}


bool isPalindrome(string sentence, int len)
{
string temp = sentence;
for (int i = 0; i == len; i++)
{
if(isalpha(sentence[i]))
temp[i] = tolower(sentence[i]);
}

for (int i = 0; i < len; i++)
{
while (ispunct(temp[i]) || isspace(temp[i]))
i++;

while (ispunct(temp[len]) || isspace(temp[len]))
len--;

if (sentence[i] != sentence[len])
return false;

len--;
}

return true;
}
Last edited on
You've opened at least 3 different threads for the same topic now. At least figure out how to use code tags, if you can't observe basic forum etiquette. Look for the <> button, and feel free to edit your OP to include them.
I dont know anything about forums
Then, this is a perfect time to learn. Trust me, people will start to take you seriously. ;D
Topic archived. No new replies allowed.