Numeric palindromes-code needed

Howdy, anyone got some code to solve a numeric palindrome in C? Help would be appreciated.

Comments

  • Hi.

    The easiest method which I can think of would be to convert the number to a string and then compare the indeces into the array.
    Here is a piece of code that I have put together which seems to work.

    [code]
    #include
    #include

    int main()
    {
    int iPalin = 12344545;
    char lpszString[256];

    //Convert the int to a string
    sprintf( lpszString, "%d", iPalin );

    //Get the length of the string
    int iLength = strlen( lpszString ) - 1;

    //Compare positions
    for( int iLoop = 0; iLoop <= iLength / 2; iLoop++ )
    {
    if( lpszString[iLoop] != lpszString[iLength-iLoop] )
    {
    cout<<"The number "<<iPalin<<" is not a Palindrome"<<endl;
    cout<<lpszString[iLoop]<<" does not match "<<lpszString[iLength-iLoop]<<endl;
    return 0;
    }
    }

    cout<<"The number "<<iPalin<<" is a Palindrome"<<endl;

    return 0;
    }
    [/code]

    It would also be possible to do it by using % and getting the individual values of the numberic value, but that gets complicated, and above is by far the easiest way.


    Ciaran McCormack
    www.ciaranmccormack.tk

    "Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." --Albert Einstein

  • : Howdy, anyone got some code to solve a numeric palindrome in C? Help would be appreciated.
    :
    :

    You will probably want to visit www.p196.org


    Greets,
    Eric Goldstein
    www.gvh-maatwerk.nl

  • : Hi.
    :
    : The easiest method which I can think of would be to convert the number to a string and then compare the indeces into the array.
    : Here is a piece of code that I have put together which seems to work.
    :
    : [code]
    : #include
    : #include
    :
    : int main()
    : {
    : int iPalin = 12344545;
    : char lpszString[256];
    :
    : //Convert the int to a string
    : sprintf( lpszString, "%d", iPalin );
    :
    : //Get the length of the string
    : int iLength = strlen( lpszString ) - 1;
    :
    : //Compare positions
    : for( int iLoop = 0; iLoop <= iLength / 2; iLoop++ )
    : {
    : if( lpszString[iLoop] != lpszString[iLength-iLoop] )
    : {
    : cout<<"The number "<<iPalin<<" is not a Palindrome"<<endl;
    : cout<<lpszString[iLoop]<<" does not match "<<lpszString[iLength-iLoop]<<endl;
    : return 0;
    : }
    : }
    :
    : cout<<"The number "<<iPalin<<" is a Palindrome"<<endl;
    :
    : return 0;
    : }
    : [/code]
    :
    : It would also be possible to do it by using % and getting the individual values of the numberic value, but that gets complicated, and above is by far the easiest way.
    :
    :
    : Ciaran McCormack
    : www.ciaranmccormack.tk
    :
    : "Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." --Albert Einstein
    :
    :Hello, thanks a bunch, this will really help. The C language itself is not difficult to understand but my teacher is hitting us with tough logic problems and is also dropping structured-programming on us like a bomb. Anyhow, thanks again.

  • : : Howdy, anyone got some code to solve a numeric palindrome in C? Help would be appreciated.
    : :
    : :
    :
    : You will probably want to visit www.p196.org
    :
    :
    : Greets,
    : Eric Goldstein
    : www.gvh-maatwerk.nl
    :

    : Hello, thanks the for link. Downloaded appropriate file. Now I just have to comprehend it! Thanks again.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories