954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with palindromes

Hello! I have an assignment in my "intro to programming class" and I am having trouble just getting started.

We are suppose to take a txt file that has several lines of text - and create a program using "fin" to test each line to determine if it is a palindrome or not.

So far I have only the very beginning....here is my code

#include <iostream>
#include <string>
#include <fstream>
using namespace std; 
int main() 
{
  ifstream fin;
  fin.open("palindromes.txt");
  if (!fin.good ()) throw "I/O error";
  
return 0;
}



I have no idea how to create a loop that will check for palindromes. Can someone give me pointers on how to get started in order to do that?

Thank you,
confused!

confused!
Newbie Poster
9 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Please use [code][/code] tags.

Try checking out the following links... palindrome programs in C++ are not at all uncommon:
http://www.codingforums.com/showthread.php?t=56285
http://sublimesoftware.blogspot.com/2005/05/generic-programming-in-c-part-3a.html

Hope this helps

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

Use this to figure out if the number (string) is a palindrome or not...
the rest, the file handling, try doing it by yourself, it is very easy.. if you have troubles doing that, let me know. :icon_wink:


#include
#include
using namespace std;


int main ()
{
int x;
string n,mem,inverse=""; //inverse is empty at the begining..
cin >> n; // enter the number we want to check if it is polindrome or not
mem=n; // store n in mem,, you will needed later...
x=n.size(); // get the size of the string and store it in the int x

while (x>0)
{
inverse=inverse+n.substr(x-1); // get's the last digit and puts it in the new string 'inverse'
n.erase(x-1,x); //erase the last digit... that's why we needed 'mem'
x--; // loop stuff
}

cout << inverse << endl; // not necessary.

if (mem == inverse) // if the entered string is the same as the inverse, then it is "palindromic"
// some examples... 123321 or ada etc.
{
cout << "The number is palindromic";
}


return 0;
}

/*****************************************************************
**The program reverses the string that was inputed by the user.
** tells us if it is polindromic or not.
**Haji Akhundov. (c) All rights reserved.:)
******************************************************************/

hajiakhundov
Newbie Poster
21 posts since Mar 2009
Reputation Points: 10
Solved Threads: 2
 

I am not showing-off, But just want to tell that how can STL come to your rescue.
The program submitted by Haji Akhundov, Can be shorten to few lines only.

#include<iostream>
#include<string>
#include<algorithm>
int main()
{
    std::string orig, rev;
    std::cin>>orig;
    rev=orig;
    std::reverse(rev.begin(), rev.end() );
    if (rev==orig)
        std::cout<<"Yes";
    else
        std::cout<<"No";
}
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

And I'd like to point out that hajiakundov and siddhant3s have both not helped confused! to learn. Giving answers only gets the assignment done, but does not promote learning how to do the assignment.

Please read the stickies at the top of this forum.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

>And I'd like to point out that hajiakundov and siddhant3s have both not helped >confused! to learn. Giving answers only gets the assignment done, but does >not promote learning how to do the assignment.

I agree. Neither do I like it. Read My post about it.
http://www.daniweb.com/forums/post813528.html#post813528
These are few quotes from in there:
Please note:
I know no one gets paid to help here. I even know that you can debug the flawed code very quickly and repost it. But the real pain lies in explaining the things to the new-bie so that he can get meaning to your code.

I was just trying to point to Haji Akhundov's post, that the things could be done in much simple manner with STL in hand. I was trying to show, the number of lines which gets reduced while using STL

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

And I'd like to point out that hajiakundov and siddhant3s have both not helped confused! to learn. Giving answers only gets the assignment done, but does not promote learning how to do the assignment.

Please read the stickies at the top of this forum.

Vmanes >> The thread was posted on Nov 26th, 2006... I think he completed the assignment by today? Don't you think so? :)

siddhant3s>> Thanks man, I'll take a look at your code ;)

hajiakhundov
Newbie Poster
21 posts since Mar 2009
Reputation Points: 10
Solved Threads: 2
 

Please don't reply to old posts, it does tend to confuse the situation. If you have a question regarding an old post, start a new thread, and feel free to refer to the old one.

That way near-sighted old farts like me don't get all riled up. ;)

After all, you did write as if responding to the OP, who, as you said, has probably long completed the assignment.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

ok, i got what you saying)
but note, when people google these kind of things, they get the links to this kind of threads... and I wouldn't propably figure out how to shorten this program using STL. :)
So, I think that everything is cool)
take care.

hajiakhundov
Newbie Poster
21 posts since Mar 2009
Reputation Points: 10
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You