I need to write a program and can't seem to get it

My job is to read in a line of data from an input file, remove all white space and punctuation, then check to see whether or not it is a palindrome. A palindrome is a word or phrase that is the same backwards as it is forward like RACECAR.
The input file is an arbitrary length long including no input. I need to read in a line of data at a time then process it until there is no more data to process.
The output should look something like this.

Input Value: Sit on a potato Pan otis
Is it a palindrome? Yes

Input Value: race car
Is it a palindrome Yes

Input Value: wooHaaaaa
Is it a palindrome? No

I am very new to Programming so anything that you smarter people can do to help me would be greatly appreciated!
This is what I have right now:


#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);
if (Palindrome)
cout << sentence << 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;
}

I have this basic program written but have no clue where to go from here
Last edited on
Seems like it works. Whats the problem?
I have this program, but I dont know how to get what I need. I need to read in each line of the input file and check if each line is a Palindrome. it should look something like this:

Input Value: Sit on a potato Pan otis
Is it a palindrome? Yes

Input Value: race car
Is it a palindrome Yes

Input Value: wooHaaaaa
Is it a palindrome? No

but I dont know how to achieve this goal
Start by stripping out the whitespace.
I'm very new to programming. Ive been reading my book but I cant figure out how to do a lot of the things that are needed including stripping out the whitespace
Here's an example that removes the letter R from whatever you type in:
1
2
3
4
5
6
7
8
9
10
11
std::string a, b;
std::cout << "Enter text to remove Rs from:" << std::endl;
std::getline(std::cin, a);
for(std::string::size_type i = 0; i < a.size(); ++i)
{
    if(a[i] != 'R' && a[i] != 'r')
    {
        b += a[i];
    }
}
std::cout << b << std::endl;
Enter text to remove Rs from:
Yarr, THAR be really scary.
Ya, THA be eally scay.
 
 
 
 
 
 
 
http://ideone.com/TuNTyM
From this you should be able to adjust it to remove spaces instead of the letter R.
Last edited on
Just a quick response to what L B said,

Since this person is using the #include <iostream> library there is no need to use std::

Hope this helps,

Prototype151
@prototype151 That is incorrect. using namespace std; allows you to omit the std:: prefix. Including the iostream header is what gives access to std::cout and std::cin in the first place.
Ive never used std::cin before.
Would someone be able to show me how I could best put it into my program or if there is another way
You've never used the standard input stream before? It's just like any other input stream, including the one in your program called inData.
Topic archived. No new replies allowed.