Test if an integer is a palindrome in c language

Discussion in 'C Programming' started by Wabz, Aug 14, 2007.

  1. Wabz

    Wabz Guest

    Hello mates,

    Does anyone know how to write a function that tests if an integer is a
    palindrome in C language?
    Wabz, Aug 14, 2007
    #1
    1. Advertising

  2. Wabz

    Kevin Handy Guest

    Wabz wrote:

    > Does anyone know how to write a function that tests if an integer is a
    > palindrome in C language?


    Yes. I'm quite sure that someone does.

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Kevin Handy, Aug 14, 2007
    #2
    1. Advertising

  3. Wabz

    Wabz Guest

    On Aug 14, 1:19 pm, Kevin Handy <> wrote:
    > Wabz wrote:
    > > Does anyone know how to write a function that tests if an integer is a
    > >palindromein C language?

    >
    > Yes. I'm quite sure that someone does.
    >
    > ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----http://www.newsfeeds.comThe #1 Newsgroup Service in the World! 120,000+ Newsgroups
    > ----= East and West-Coast Server Farms - Total Privacy via Encryption =----


    Could this someone possibly help me write the said program?
    Wabz, Aug 14, 2007
    #3
  4. Wabz

    Flash Gordon Guest

    Wabz wrote, On 14/08/07 18:42:
    > On Aug 14, 1:19 pm, Kevin Handy <> wrote:
    >> Wabz wrote:
    >>> Does anyone know how to write a function that tests if an integer is a
    >>> palindromein C language?

    >> Yes. I'm quite sure that someone does.
    >>
    >> ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----http://www.newsfeeds.comThe #1 Newsgroup Service in the World! 120,000+ Newsgroups
    >> ----= East and West-Coast Server Farms - Total Privacy via Encryption =----


    Please don't quote things you are not commenting on, such as the
    annoying text appended to Kevin's post.

    > Could this someone possibly help me write the said program?


    Yes, I'm sure one of the people meeting those requirements could help if
    they were so desired. However, knowledgeable people are unlikely to do
    your homework for you when you have not even made an attempt. So if you
    want help you should attempt it and then post your attempt here with any
    questions you have. The more effort you put in the more people will be
    prepared to help you.
    --
    Flash Gordon
    Flash Gordon, Aug 14, 2007
    #4
  5. Wabz

    osmium Guest

    "Wabz" writes:

    > On Aug 14, 1:19 pm, Kevin Handy <> wrote:
    >> Wabz wrote:
    >> > Does anyone know how to write a function that tests if an integer is a
    >> >palindromein C language?

    >>
    >> Yes. I'm quite sure that someone does.


    > Could this someone possibly help me write the said program?


    Start by extracting the individual digits of the integer. You may find the
    modulo operator, %, helpful. There is another way, involving sprintf() in
    <stdio.h>, but if this is a school assignment, the instructor probably
    expects the modulo way.
    osmium, Aug 14, 2007
    #5
  6. "Wabz" <> wrote in message
    news:...
    > Hello mates,
    >
    > Does anyone know how to write a function that tests if an integer is a
    > palindrome in C language?
    >

    What base? Eleven is a palindrome when written in base 10,
    but not when written in base 8.
    Create a buffer large enough to hold the characters to display the
    largest integer. Use sprintf to fill it. Check whether palindrome.
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Aero Stability and Controls Computing
    Fred Kleinschmidt, Aug 14, 2007
    #6
  7. Wabz

    Army1987 Guest

    On Tue, 14 Aug 2007 17:10:51 +0000, Wabz wrote:

    > Hello mates,
    >
    > Does anyone know how to write a function that tests if an integer is a
    > palindrome in C language?

    I assume you mean palindrome in base 10.
    sprintf() it to a string, and then check whether the string is
    palindrome:
    len = strlen(str);
    for (i = 0; i < len/2; i++) {
    if (str != str[len - i - 1])
    break; /*it is not palindome*/
    }

    --
    Army1987 (Replace "NOSPAM" with "email")
    No-one ever won a game by resigning. -- S. Tartakower
    Army1987, Aug 14, 2007
    #7
  8. Wabz

    Kevin Handy Guest

    Wabz wrote:

    > Could this someone possibly help me write the said program?


    Should have asked that in the first place.

    Many ways of doing it. You need to decide
    what number system you want to
    use: binary, hex, octal, decimal, etc.

    1. Convert to string in that format, and compare
    front end of the resulting string to the back end.

    2. A number can be a palindrome in one base, while
    not being one in another. If you can choose your
    base for a specific number N, you can pick
    base (N-1), which gives you the result (11),
    which is always a palindrome. So your program
    could then be hard coded to always print "true".

    3. Shift digits of N into a new value, in reverse
    order (% helps here). Compare N and reverse N.

    ....

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Kevin Handy, Aug 14, 2007
    #8
  9. Wabz

    user923005 Guest

    On Aug 14, 11:20 am, Army1987 <> wrote:
    > On Tue, 14 Aug 2007 17:10:51 +0000, Wabz wrote:
    > > Hello mates,

    >
    > > Does anyone know how to write a function that tests if an integer is a
    > > palindrome in C language?

    >
    > I assume you mean palindrome in base 10.


    Otherwise (for instance) FFFF is a palindrome, but 65535 will say
    'nope'

    > sprintf() it to a string, and then check whether the string is
    > palindrome:
    > len = strlen(str);
    > for (i = 0; i < len/2; i++) {
    > if (str != str[len - i - 1])
    > break; /*it is not palindome*/
    >
    > }


    It might be interesting to try every base from 2 to 36.
    Given that condition, what percentage of integers are palindromes?
    user923005, Aug 14, 2007
    #9
  10. Wabz

    user923005 Guest

    On Aug 14, 2:15 pm, user923005 <> wrote:
    > On Aug 14, 11:20 am, Army1987 <> wrote:
    >
    > > On Tue, 14 Aug 2007 17:10:51 +0000, Wabz wrote:
    > > > Hello mates,

    >
    > > > Does anyone know how to write a function that tests if an integer is a
    > > > palindrome in C language?

    >
    > > I assume you mean palindrome in base 10.

    >
    > Otherwise (for instance) FFFF is a palindrome, but 65535 will say
    > 'nope'
    >
    > > sprintf() it to a string, and then check whether the string is
    > > palindrome:
    > > len = strlen(str);
    > > for (i = 0; i < len/2; i++) {
    > > if (str != str[len - i - 1])
    > > break; /*it is not palindome*/

    >
    > > }

    >
    > It might be interesting to try every base from 2 to 36.
    > Given that condition, what percentage of integers are palindromes?


    It seems that what numbers are not palindromes in some base might be a
    more interesting question. Of course if we include bases up to that
    number, then the answer is obviously 'none'.

    #include <stdlib.h>
    #include <string.h>
    /* ltostr is from snippets (I fixed the broken bit). */
    char *ltostr(long long num, char *string, size_t max_chars, unsigned
    base)
    {
    char remainder;
    int sign = 0;
    if (base < 2 || base > 36)
    return ((void *) 0);
    if (num < 0) {
    sign = 1;
    num = -num;
    }
    if (num == 0) /* bugbug:drc formerly wrong result here... */
    return "0";
    string[--max_chars] = '\0';
    for (max_chars--; max_chars > sign && num != 0; max_chars--) {
    remainder = (char) (num % base);
    if (remainder < 9)
    string[max_chars] = remainder + '0';
    else
    string[max_chars] = remainder - 10 + 'A';
    num /= base;
    }
    if (sign)
    string[--max_chars] = '-';
    if (max_chars > 0)
    memset(string, ' ', max_chars + 1);
    return string + max_chars;
    }

    int ispal(const char *start)
    {
    const char *end = start + strlen(start) - 1;
    while (end > start) {
    if (*start != *end) {
    return 0;
    }
    start++;
    end--;
    }
    return 1;
    }

    #include <stdio.h>
    static char string[50] = {0};
    int main(void)
    {
    long long index;
    unsigned base;
    for (index = 0; index < 4000000000; index++) {
    for (base = 2; base < 37; base++) {
    long long val = index;
    char *p = ltostr(val, string, sizeof string,
    base);
    if (isspace(*p))
    p++;
    if (ispal(p)) {
    printf("%llu = %s is a palindrome in base %u\n", val,
    p, base);
    }
    }
    }
    return 0;
    }
    user923005, Aug 14, 2007
    #10
  11. Wabz

    user923005 Guest

    /* Original ltostr() was hosed. Fixed version: */
    static const char B36TAB[] =
    {
    '0', '1', '2', '3', '4', '5', '6', '7', '8',
    '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
    'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
    'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
    };
    #include <stdlib.h>
    #include <string.h>
    /* ltostr is from snippets (I fixed the broken bit). */
    char *ltostr(long long num, char *string, size_t max_chars, unsigned
    base)
    {
    char remainder;
    int sign = 0;
    if (base < 2 || base > 36)
    return ((void *) 0);
    if (num < 0) {
    sign = 1;
    num = -num;
    }
    if (num == 0) /* bugbug:drc formerly wrong result
    here... */
    return "0";
    string[--max_chars] = '\0';
    for (max_chars--; max_chars > sign && num != 0; max_chars--) {
    remainder = (char) (num % base);
    string[max_chars] = B36TAB[remainder];
    num /= base;
    }
    if (sign)
    string[--max_chars] = '-';
    if (max_chars > 0)
    memset(string, ' ', max_chars + 1);
    return string + max_chars;
    }

    int ispal(const char *start)
    {
    const char *end = start + strlen(start) - 1;
    while (end > start) {
    if (*start != *end) {
    return 0;
    }
    start++;
    end--;
    }
    return 1;
    }

    #include <stdio.h>
    static char string[50] =
    {0};
    int main(void)
    {
    long long index;
    unsigned base;
    for (index = 0; index < 4000000000; index++) {
    for (base = 2; base < 37; base++) {
    long long val = index;
    char *p = ltostr(val, string, sizeof string,
    base);
    if (isspace(*p))
    p++;
    if (ispal(p)) {
    printf("%llu = %s is a palindrome in base %u\n", val,
    p, base);
    }
    }
    }
    return 0;
    }
    /*
    Sample output:
    ....
    2290134 = E656E is a palindrome in base 20
    2290141 = 84548 is a palindrome in base 23
    2290156 = 1IEI1 is a palindrome in base 35
    2290187 = 9H1H9 is a palindrome in base 22
    2290220 = 36Q63 is a palindrome in base 29
    2290252 = 11022100122011 is a palindrome in base 3
    2290293 = 50805 is a palindrome in base 26
    2290322 = 2OOO2 is a palindrome in base 30
    2290353 = 925529 is a palindrome in base 12
    2290378 = 4270724 is a palindrome in base 9
    2290391 = HAHAH is a palindrome in base 19
    2290460 = BG6GB is a palindrome in base 21
    2290499 = 2ERE2 is a palindrome in base 31
    2290501 = 1D3D1 is a palindrome in base 36
    2290530 = 5LEL5 is a palindrome in base 25
    2290534 = E666E is a palindrome in base 20
    2290627 = 3K9K3 is a palindrome in base 28
    2290641 = 1000101111001111010001 is a palindrome in base 2
    2290658 = 20233033202 is a palindrome in base 4
    2290670 = 84648 is a palindrome in base 23
    2290671 = 9H2H9 is a palindrome in base 22
    2290686 = 6LGL6 is a palindrome in base 24
    2290735 = 13EE31 is a palindrome in base 18
    2290738 = 48A84 is a palindrome in base 27
    2290752 = HAIAH is a palindrome in base 19
    2290850 = 25T52 is a palindrome in base 32
    2290853 = 1O9O1 is a palindrome in base 34
    2290901 = BG7GB is a palindrome in base 21
    2290922 = 2290922 is a palindrome in base 10
    ....
    */
    user923005, Aug 14, 2007
    #11
  12. user923005 <> writes:

    > /* Original ltostr() was hosed. Fixed version: */


    <string-based palindrome tester snipped>

    I like this way:

    int pal_aux(int n, int r, int b)
    {
    return n == 0 ? r : pal_aux(n / b, r * b + n % b, b);
    }

    int is_pal_in_base(int n, int b)
    {
    return pal(n, 0, b) == n;
    }

    (not well tested, but it *looks* sound!)

    --
    Ben.
    Ben Bacarisse, Aug 14, 2007
    #12
  13. Wabz

    user923005 Guest

    On Aug 14, 3:33 pm, Ben Bacarisse <> wrote:
    > user923005 <> writes:
    > > /* Original ltostr() was hosed. Fixed version: */

    >
    > <string-based palindrome tester snipped>
    >
    > I like this way:
    >
    > int pal_aux(int n, int r, int b)
    > {
    > return n == 0 ? r : pal_aux(n / b, r * b + n % b, b);
    > }
    >
    > int is_pal_in_base(int n, int b)
    > {
    > return pal(n, 0, b) == n;


    /*Did you mean:*/
    return pal_aux(n, 0, b) == n;

    > }
    >
    > (not well tested, but it *looks* sound!)


    Certainly much brighter than converting to character.
    But it makes it harder to see the answers.
    user923005, Aug 14, 2007
    #13
  14. user923005 <> writes:

    > On Aug 14, 3:33 pm, Ben Bacarisse <> wrote:
    >> user923005 <> writes:
    >> > /* Original ltostr() was hosed. Fixed version: */

    >>
    >> <string-based palindrome tester snipped>
    >>
    >> I like this way:
    >>
    >> int pal_aux(int n, int r, int b)
    >> {
    >> return n == 0 ? r : pal_aux(n / b, r * b + n % b, b);
    >> }
    >>
    >> int is_pal_in_base(int n, int b)
    >> {
    >> return pal(n, 0, b) == n;

    >
    > /*Did you mean:*/
    > return pal_aux(n, 0, b) == n;


    Yes. Thanks for spotting it that.

    One day I'll remember just to paste the code, and to "tidy it up" for
    posting. (I added the _aux because I thought "pal" rather too sort a
    name, even for a small helper function.)

    --
    Ben.
    Ben Bacarisse, Aug 14, 2007
    #14
  15. Wabz

    CBFalconer Guest

    Wabz wrote:
    > Kevin Handy <> wrote:
    >> Wabz wrote:
    >>
    >>> Does anyone know how to write a function that tests if an integer
    >>> is a palindromein C language?

    >>
    >> Yes. I'm quite sure that someone does.

    >
    > Could this someone possibly help me write the said program?


    I suggest you first find him and then ask him (or her). An
    alternative would be to do your own homework.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>


    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Aug 15, 2007
    #15
  16. Wabz

    pete Guest

    Wabz wrote:
    >
    > Hello mates,
    >
    > Does anyone know how to write a function that tests if an integer is a
    > palindrome in C language?


    /* BEGIN new.c */

    #include <stdio.h>
    #include <string.h>
    #include <limits.h>

    int integer_is_palindrome(long unsigned integer);
    char *str_rev(char *s);

    int main(void)
    {
    int integer;

    for (integer = 0; 1000 > integer; ++integer) {
    if (integer_is_palindrome(integer)) {
    printf("%d\n", integer);
    }
    }
    return 0;
    }

    int integer_is_palindrome(long unsigned integer)
    {
    char lutoa_buff[(sizeof(long) * CHAR_BIT) / 3 + 1];
    char copy[sizeof lutoa_buff];

    sprintf(lutoa_buff, "%lu", integer);
    strcpy(copy, lutoa_buff);
    str_rev(copy);
    return strcmp(copy, lutoa_buff) == 0;
    }

    char *str_rev(char *s)
    {
    char *const p = s;
    char *t = p;
    char swap;

    if (*t != '\0' && *++t != '\0') {
    t += strlen(t + 1);
    do {
    swap = *t;
    *t-- = *s;
    *s++ = swap;
    } while (t > s);
    }
    return p;
    }

    /* END new.c */


    --
    pete
    pete, Aug 15, 2007
    #16
  17. Wabz

    Richard Guest

    pete <> writes:

    > Wabz wrote:
    >>
    >> Hello mates,
    >>
    >> Does anyone know how to write a function that tests if an integer is a
    >> palindrome in C language?

    >
    > /* BEGIN new.c */
    >
    > #include <stdio.h>
    > #include <string.h>
    > #include <limits.h>
    >
    > int integer_is_palindrome(long unsigned integer);
    > char *str_rev(char *s);
    >
    > int main(void)
    > {
    > int integer;
    >
    > for (integer = 0; 1000 > integer; ++integer) {
    > if (integer_is_palindrome(integer)) {
    > printf("%d\n", integer);
    > }
    > }
    > return 0;
    > }
    >
    > int integer_is_palindrome(long unsigned integer)
    > {
    > char lutoa_buff[(sizeof(long) * CHAR_BIT) / 3 + 1];
    > char copy[sizeof lutoa_buff];
    >
    > sprintf(lutoa_buff, "%lu", integer);
    > strcpy(copy, lutoa_buff);
    > str_rev(copy);
    > return strcmp(copy, lutoa_buff) == 0;
    > }
    >
    > char *str_rev(char *s)
    > {
    > char *const p = s;
    > char *t = p;
    > char swap;
    >
    > if (*t != '\0' && *++t != '\0') {
    > t += strlen(t + 1);
    > do {
    > swap = *t;
    > *t-- = *s;
    > *s++ = swap;
    > } while (t > s);
    > }
    > return p;
    > }
    >
    > /* END new.c */


    slow?

    something like this for the palindrome check on the string. Untested and
    pretty much pseudo code.

    len=strlen(p);
    endp=p+len-1;
    while(endp>p)
    if ((*endp--)!=(*p++))
    return 0;
    return 1;



    --
    Richard, Aug 15, 2007
    #17
  18. Wabz

    Ben Pfaff Guest

    user923005 <> writes:

    > It might be interesting to try every base from 2 to 36.
    > Given that condition, what percentage of integers are palindromes?


    I don't have an answer, but a few moments of arithmetic led me to
    discover that in base B there are pow(B, -N/2) palindromic numbers
    of length N, for even N. Somehow this is pleasing, even though
    it's almost trivial.
    --
    Ben Pfaff
    http://benpfaff.org
    Ben Pfaff, Aug 15, 2007
    #18
  19. Ben Pfaff wrote:
    > user923005 <> writes:
    >
    >> It might be interesting to try every base from 2 to 36.
    >> Given that condition, what percentage of integers are palindromes?

    >
    > I don't have an answer, but a few moments of arithmetic led me to
    > discover that in base B there are pow(B, -N/2) palindromic numbers
    > of length N, for even N. Somehow this is pleasing, even though
    > it's almost trivial.


    What you calculated is the ratio of palindromic numbers in all numbers of
    length N, not the amount of palindromic numbers of length N. In base 10,
    there are not 0.1 but 9 palindromic numbers of length 2. I'm not sure
    whether you meant to post an expression that would return 9, or whether you
    meant to describe your expression as returning the ratio.
    Harald van =?UTF-8?B?RMSzaw==?=, Aug 15, 2007
    #19
  20. Wabz

    Ben Pfaff Guest

    Harald van Dijk <> writes:

    > Ben Pfaff wrote:
    >> user923005 <> writes:
    >>
    >>> It might be interesting to try every base from 2 to 36.
    >>> Given that condition, what percentage of integers are palindromes?

    >>
    >> I don't have an answer, but a few moments of arithmetic led me to
    >> discover that in base B there are pow(B, -N/2) palindromic numbers
    >> of length N, for even N. Somehow this is pleasing, even though
    >> it's almost trivial.

    >
    > What you calculated is the ratio of palindromic numbers in all numbers of
    > length N, not the amount of palindromic numbers of length N. In base 10,
    > there are not 0.1 but 9 palindromic numbers of length 2. I'm not sure
    > whether you meant to post an expression that would return 9, or whether you
    > meant to describe your expression as returning the ratio.


    I meant the ratio. Oops.
    --
    "I hope, some day, to learn to read.
    It seems to be even harder than writing."
    --Richard Heathfield
    Ben Pfaff, Aug 15, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lorin Leone

    Palindrome (HELP)

    Lorin Leone, Nov 12, 2003, in forum: C++
    Replies:
    4
    Views:
    995
    Chris Theis
    Nov 13, 2003
  2. Runic911

    Palindrome

    Runic911, Nov 13, 2003, in forum: Python
    Replies:
    24
    Views:
    1,566
    Andrew Dalke
    Nov 15, 2003
  3. Tim Churches

    Re: Re: Palindrome

    Tim Churches, Nov 13, 2003, in forum: Python
    Replies:
    2
    Views:
    723
    yousafzai
    Jun 5, 2011
  4. Pierre Quentel

    Re: Palindrome

    Pierre Quentel, Nov 13, 2003, in forum: Python
    Replies:
    2
    Views:
    498
    Francis Avila
    Nov 13, 2003
  5. cat_dog_ass

    Palindrome using StringBuffer

    cat_dog_ass, Jan 23, 2007, in forum: Java
    Replies:
    4
    Views:
    2,885
    abhi2varma
    Jan 5, 2013
Loading...

Share This Page