The problem:

Palindromes are words or phrases that read the same backwards as forwards. Write a C++ program that indicates whether each line on an input file, called sentences.txt and given on my homepage, contains a palidrome. Those lines of sentences.txt that are palidromes should be written to an output file, called palindromes.txt.

The input file given:

Vanna, wanna V?
Must sell at tallest sum.
I should get an eighty. Why? Because I get an eighty in all my other classes.
Lager, Sir, is regal.
I don't need documentation. The code is obvious to me. OK, but is it to the next programmer?
Evil olive.
I never thought computer science could be such fun!
Sex at noon taxes.
You expect me to read ten chapters in two days? No, I expect you to read ten chapters over the semester.
Never odd or even.

(all sentences are on one line)

My code:

#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("sentences.txt");
	outData.open("palindromes.txt");

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

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

	getline(inData, sentence);
	while(inData)
	{
		len = sentence.length();
		len--;		//Accounting for an array storing a value in a zero position

		Palindrome = isPalindrome(sentence, len);
		if (Palindrome)
			cout << sentence << endl;
		
		getline(inData, sentence);
	}
	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]);
			//Stores all characters as lowercase in a temporary string
	}

	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;
}

Keep in mind for now I'm just testing with cout rather than the output file until I get it to work.

The issue is that it only prints the first sentence and I'm not entirely sure why. I'm pretty sure it isn't something wrong with obtaining the sentence input because of a test that I did earlier, but suspect that something might be wrong with my function to test whether or not the sentence is a palindrome.

My exact output:

Vanna, wanna V?

Any help would be greatly appreciated, I have a feeling it's just a simple error on my part.

Recommended Answers

All 29 Replies

Between line 42 and 43 put a check statement to confirm appropriate read from file. Maybe something like:
cout << "file read is: " << sentence << endl;

In isPalindrome() strip the punctuation and spaces from temp before testing. Don't just advance indexes.

Use i to be the letter in front to test and use j to be letter in back to test. i starts at zero. j starts at length of temp when all spaces and punctuation have been removed minus one. Each time through the loop increment i and decrease j by one. The loop should stop when i is one half the starting length of temp after spaces and punctuation removed.

Hello.
It would appear when I compiled it to try and help with some errors that there are not any! Are you sure your compiling properly?

Do you mean that when you compile the code it gives you the correct output?

The output should look like this:

Vanna, wanna V?
Must sell at tallest sum.
Lager, Sir, is regal.
Evil olive.
Sex at noon taxes.
Never odd or even.

And I also tried as the other poster suggested in stripping the spaces and punctuation before testing, but this is presenting another problem for me.

I don't understand why this doesn't work though, if it works for one and my end of file loop works correctly (just tested it again) what could be causing it to only print the first one?

Vanna, wanna V?
Must sell at tallest sum.
Lager, Sir, is regal.
Evil olive.
Sex at noon taxes.
Never odd or even.

Do you want it just to say that or for it to be options, where you have to type keys to get something? :)

Throw in the output statements after each file read, after each conversion to lower case, after each removal punctuation/spaces (if you go that route) and output each letter before it is compared. Continue this type of investigation with other variables if need be until you pinpoint the problem.

Yeah I just need it to say that exactly, no user input required other than what is in the input file.

From what I understand you want it just to say:

Vanna, wanna V?
Must sell at tallest sum.
I should get an eighty. Why? Because I get an eighty in all my other classes.
Lager, Sir, is regal.
I don't need documentation. The code is obvious to me. OK, but is it to the next programmer?
Evil olive.
I never thought computer science could be such fun!
Sex at noon taxes.
You expect me to read ten chapters in two days? No, I expect you to read ten chapters over the semester.
Never odd or even.

In which case the code should be:

#include <iostream>            
int main ()
{
std::cout<< "Vanna, wanna V?zn";
std::cout<< "\nMust sell at tallest sum.\n";
std::cout<< "\nI should get an eighty. Why? Because I get an eighty in all my other classes.\n";
std::cout<< "\nLager, Sir, is regal.\n";
std::cout<< "\nI dont need documentation. The code is obvious to me. OK, but is it to the next programmer?\n";
std::cout<< "\nEvil Olive\n";
std::cout<< "\nI never thought computer science could be such fun!\n";
std::cout<< "\nSex at noo taxes.";
std::cout<< "\nYou expect me to read ten chapters in two days? No, I expect you to read ten chapters over the semester.\n";
std::cout<< "\nNever odd or even.\n";
std::cin.get ();
return 0;
}

Try that. I did this using CMD (command prompt). Compile this and see if what you wanted. If its ok, click solved thread so others know its done! (=

Ok so after doing some quick checks it doesn't look like my temp is actually storing the characters as lower case. This explains why the first sentence works, because it begins and ends with a capital letter.

So I just need to figure out how to actually get my temp string to be lower case...

Is the code i posted correct for your purposes?

>>(all sentences are on one line)

why?

>> Write a C++ program that indicates whether each line on an input file...

Thanks zobadof but I think you misunderstood what I need.

What you posted there is the input file, I need the program to check if each sentence is a palindrome or not and if so, print it to an output file.

So the output needs to end up being just exactly as my post shows above.

I feel like it's pretty much complete I'm just missing something in the way of getting all letters to be lowercase.

Sorry. From what i gathered he/she wanted it so that it just said it and didnt need any user input!

you just need all letters to be lowercase rather than UPPERCASE?

Sorry Firstperson my mistake I was typing up the problem quickly. I meant that each sentence is on its own line.

And to get my program to work, yes, I believe it is just a matter of getting my sentence into all lowercase letters.

Probably a pretty easy task but I'm a rookie when it comes to string operations so any help would be greatly appreciated.

Is it all sorted now or do you want it all to lowercase?

{
	string sentence;
	bool palindrome;
	int len;
		
	ifstream indata;
	ofstream outdata;

	indata.open("sentences.txt");
	outdata.open("palindromes.txt");

	if (!indata)
	{
		cout << "can't open input file successfully";
		return 1;
	}

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

	getline(indata, sentence);
	while(indata)
	{
		len = sentence.length();
		len--;					//Accounting for an array storing a value in a zero position

		palindrome = ispalindrome(sentence, len);
		if (palindrome)
			cout << sentence << endl;
		
		getline(indata, sentence);
	}
	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]);
								//Stores all characters as lowercase in a temporary string
	}

	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 think i have understood you this time, there is no UPPERCASE all lowercase. If this is solved, click thread solved so other people dont comment when unneeded! I hope i have helped you!

Helpful tips :

create a function that :

1) Returns the lower or uppered case of a string
2) Removes all punctuation from the string
3) Removes all spaces from the string

Then what you are left with is something like this for example :

< Original string > Vanna, wanna V?
< after going through the functions above > vannawannav

Then all you need to do is check the (last - n) character with the (first+n)
character.

Sorry to have to say this. Puncuation and spaces can sometimes be very important in codes!!

Sorry to have to say this. Puncuation and spaces can sometimes be very important in codes!!

when checking for palindrome, it does not matter.

Either matmeister has logged off or has fixed the problem. And sorry (ok) firstPerson! If this thread is solved it needs to be clicked (solved thread) so we know not to keep commenting!

OK so I'm trying to convert my string to lower case. Shouldn't this get the job done:

for (int i = 0; i == len; i++)
	{
		if(isalpha(sentence[i]))
			temp[i] = tolower(temp[i]);
								//Stores all characters as lowercase in a temporary string
	}

It would appear that matmeister has gone off. IF this thread is solved matmeister please say so as we know not to try and help anymore! :)

It hasn't been solved yet.

OK so I'm trying to convert my string to lower case. Shouldn't this get the job done:

for (int i = 0; i == len; i++)
	{
		if(isalpha(sentence[i]))
			temp[i] = tolower(temp[i]);
								//Stores all characters as lowercase in a temporary string
	}

Fix :

for (int i = 0; i < len; i++){  
     temp[i] = tolower(temp[i]);								
}

No need to check isalpha.

matmeister if your still online. Have you fixed your problem now or is there another one/etc occouring?

commented: Relax, so eager to help. Let him post when he is ready +2

Thank you so much, finally got it to work now. Thanks for the help everyone!

Thats ok! No worries, if you need some more help another time just PM me and I'll try get back to you A.S.A.P

Thats ok! No worries, if you need some more help another time just PM me and I'll try get back to you A.S.A.P

We don't do that here. From the rulebook:

Do not post asking for an answer to be sent to you via email or PM. Problems and their responses assist others who read them.

I strongly advice you to start reading the rules.

Well, actually I said if he/she needs help, I will always try through PM or forum!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.