Howdy, Stranger!

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

Categories

Numeric palindromes-code needed

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

Comments

  • GiantGiant Member Posts: 225
    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

  • tsagldtsagld Member Posts: 621
    : 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

  • Jonov93Jonov93 Member Posts: 3
    : 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.

  • Jonov93Jonov93 Member Posts: 3
    : : 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.

  • Chris BrownChris Brown USAMember Posts: 4,496 ✭✭

    ___ | http://forcoder.org | free ebooks and video tutorials about | Scratch, JavaScript, Assembly, Swift, Python, C, Java, Ruby, Delphi, Visual Basic, PHP, C#, PL/SQL, Go, Objective-C, Visual Basic .NET, Perl, R, MATLAB, C++ ABAP, FoxPro, ML, VBScript, Julia, Rust, Clojure, Kotlin, Hack, Awk, COBOL, Scheme, F#, SAS, Logo, Scala, Apex, Dart, LabVIEW, Bash, Ada, Crystal, Fortran, Erlang, Lisp, Lua, Prolog, Alice, D, Transact-SQL | ____

Sign In or Register to comment.