Thread: Palindrome

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    21

    Palindrome

    hi everybody

    question: Can anyone help me out to make a program that checks a string for being a palindrome or not

    I only want to get the logic, so that I could try it myself

    thanx in advance

    bye

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I only want to get the logic, so that I could try it myself
    Compare the first letter with the last letter

    If they match, move in one letter from each end and repeat

    If you meet in the middle, and its still equal, its a palindrome

  3. #3
    If you really want to be masochistic, you can push all the letters in a stack pop them all off into a new stirng and then use strcmp. If its == 0, its a palindrome
    DrakkenKorin

    Get off my Intarweb!!!!

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Here's one way of doing it.....

    Extra brownie points if you understand it...... lol
    This was written by richard heathfield,one of the authors of c unleashed.... enjoy....
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    #define p void
    #define   a sizeof
    #define      l char
    #define        i return
    #define          n int
    #define           d size_t
    #define            r if
    #define             o while
    #define              m else
    #define              e for
    #define              ob putchar
    #define              fu strlen
    #define             sc isalpha
    #define             at tolower
    #define            ed fgets
    #define          cp printf
    #define        ro strchr
    #define     gr main
    #define am stdin
    n c(l*s){n k=
    0;r(s){d x
    =fu (s)
    ;r(x>1)
    {l*t=s,
    *u=s+x-
    1;k=1;o
    (k&&t<u
    ){r(sc(
    *t)){r(
    sc(*u))
    
        {r(at(
       *t)!=at(
      *u)    ){k
     =0;      }m{
    ++t;--u;}}m{--
    u;}}m{++t;}}}}
    i k        ;}n
    gr(        p){
    l b        [01
    <<1        <<1
    
    <<1
    <<1
    <<1
    <<1
    <<1
    <<1
    <<1
    ],*
    z,*s=" adeil"
    "mnoprst.\n";
    
    n f[]={00,01<<1
    <<1,(1<<1<<1<<1
          )|(
          1<<
          1)|
          1},
          v[]
          ={0
    ,(1<<1<<1)|(1<<
    1)|1,1<<1<<1<<1
    
    
    ,(1         <<1
    <<01        <<1
    )|(01       <<1
    <<1 )}      ,q[
    ]={  (1     <<1
    )&1   ,1    <<1
    >>1    ,1   >>1
    ,(1     <<  1<<
    1<<      1) |1,
    1<<       01<<1
    
    
    >>1>>1,(1<<1
    <<1       )|(
    1<<        1>>
    1),         1<<
    1<<         1,(
    1<<         1<<
    1)|         (01
    <<1        )|1
    ,01       <<1
    ,(1<<1<<1<<1
    
    
    )|(1<<01),1
    <<1      <<1
    <<1       ,(1
    <<1       <<1
    )|1      <<1
    ,(1   <<1
    )|1,(1<<
    1<<   1<<
    1)|    (01
    <<1     <<1
    )|1      ,(1
    <<1       <<1
    
    
       <<1)|(1<<
     1<<       1)|
    (01         <<1
    )};         d y
    ;o(         ed(
    b,a         (b)
    ,am         )){
    {z=         ro(
     b,1        *1*
       '\n');}if(
    
    z){          *z=
    00;}        {cp(
    "%s",      b);}e
    (y=0;1    *y<a f
    /a( 1[f  ]); y++
    )ob  (y[f][  s])
    ;r(   !c(b   ))e
    (y=    0;    y<a
    v/a          1[v
    ];y          ++)
    
    
    ob(01*y[v][s]);e
    (y=0/1*1/1<<1<<1
    <<1
    ;y<
    a q
    /a 1[q]
    ;y++)ob
    (y[
    q][
    s])
    ;}i 0>>1<<0>>1<<
    1*1/(1<<1<<01);}
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Talking Gee

    Gee, does that code really work? I'll have to try when I get home

    PS: What does obfuscated mean?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    That code Stoned Coder posted is the very definition of obfuscated (to make so difficult/confusing its hard to understand).

    And those define's aren't kidding!
    DrakkenKorin

    Get off my Intarweb!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in Recursive String Palindrome Code
    By clegs in forum C Programming
    Replies: 13
    Last Post: 12-21-2008, 12:36 PM
  2. Is it a Palindrome?
    By xp5 in forum C Programming
    Replies: 3
    Last Post: 09-06-2007, 05:26 AM
  3. bool palindrome definition
    By justinc911 in forum C++ Programming
    Replies: 3
    Last Post: 11-26-2003, 05:50 PM
  4. Palindrome Coding trouble
    By TheLoneWolf32 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 07:05 PM
  5. palindrome HELP !!!
    By TED22_8 in forum C Programming
    Replies: 23
    Last Post: 01-22-2002, 02:14 PM