Really Need Help in Palindrome Strings

This is a discussion on Really Need Help in Palindrome Strings within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> #include <fstream> using namespace std; main() { char strn[80]; int i,j,len; ifstream myFile("input.txt"); while(myFile.getline(strn,80)){ cout<<strn<<endl; bool flag=false; ...

  1. #1
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59

    Really Need Help in Palindrome Strings

    Code:
    #include <iostream.h>
    #include <fstream>
    using namespace std;
    
    main()
    {
        char strn[80];
        int i,j,len;
        
         
        ifstream myFile("input.txt");
        
        while(myFile.getline(strn,80)){
        cout<<strn<<endl;
        
        bool flag=false;
        
        len=strlen(strn);
        for(i=0,j=len-1;i<=(len/2);i++,j--)
        {
        if(strn[i]==strn[j])
        flag=true;
        }
        if(flag)
           {
                 cout<<"Palindrome"<<endl<<endl;
                 }
                 else
                 {
                     cout<<"Not a palindrome"<<endl<<endl;
                     }
    }
    cout<<endl;
    system("pause");
    }
    can someone correct the code for me ? because i got a little bit wrong answers of my program . . .

    the word from the .txt is

    A car, a man, a maraca. =it is a palindrome
    Palindromelist =it is not palindrome
    Your Ad Here =it is not palindrome
    A Toyota’s a Toyota. = palindrome
    Animal loots foliated detail of stool lamina. =palindrome
    Was it a cat I saw? = it is palindrome also
    A word, phrase, verse, or sentence that reads the same backward or forward = not a palindrome
    1001 = palindrome. .

    but at my output of my program . . .

    Your Ad here is a palindrome

    A word, phrase, verse, or sentence that reads the same backward or forward = is a palindrome

    and that 2 sentence must not be palindrome it must be not a palindrome
    i got 2 wrong answer . ..plz i really need help . .

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    You should start with flag is true, then set it to false (and exit your loop) when you find the first pair of characters that fail the test.

    As it stands, your test only works to find ANY pair of letters which match, not ALL of them.

    Oh, and your indentation still sucks.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it a Palindrome?
    By xp5 in forum C Programming
    Replies: 3
    Last Post: 09-06-2007, 05:26 AM
  2. Palindrome in strings
    By JFonseka in forum C Programming
    Replies: 4
    Last Post: 08-31-2007, 06:40 AM
  3. strings and palindrome's
    By watshamacalit in forum C Programming
    Replies: 6
    Last Post: 01-07-2003, 05:17 PM
  4. Palindrome
    By Nicholas35 in forum C++ Programming
    Replies: 7
    Last Post: 02-07-2002, 09:06 PM
  5. palindrome HELP !!!
    By TED22_8 in forum C Programming
    Replies: 23
    Last Post: 01-22-2002, 01:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21