Thread: palindrome

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    30

    palindrome

    i've been trying to make this function work... but to no avail


    Code:
    pal (int *str)
    {
       int i=0,k,flag;
    
       char x[10];
    
       while(*(str+i)!='\0')
       {
    	i++;
       }
       printf("%d", i);
    
        k=i-1;
        i=0;
    
       while( k>=0)
       {
         x[i]=*(str+k);
         printf("%c", *(str+k));
         k--; i++;
       }
       flag=0;
       k=i;
       i=0;
    
       while(*(str+i)!= '\0')
       {
        if (*(str+i) != x[i])
        {
        flag=1;
        break;
        }
        i++; k--;
       }
    
       if(flag==0)
       {
          printf("Palindrome");
       }
    
    
       if (flag==1)
       {
          printf("Not Palindrome");
       }
      
    }

    Can anyone help me plz

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's an easy way:
    Code:
    for( x = 0, y-1 = strlen( foo ); x < strlen( foo ); x++,y-- )
    {
        if( foo[x]!=foo[y] )
            printf( "Not a palindrome." );
    }
    You could of course optimize this to use only half the length, but that's the easiest way. Of course, this doesn't count or allow for white space, and it doesn't check case sensitivity.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    27
    You have declared str as int *
    This could be ok, but then you would have to cast it to char
    before comparing with '\0'
    like

    while(((char)*(str+i))!= '\0')

    easiest would be to change int *str to char *str

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