Thread: palindrom checker

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    53

    palindrom checker 12

    i need to write a function that inputs a number and tells if its palindrom or not
    i wrote a code which calculates the length of the number and gets each cycle the digit

    how to continue
    ?
    Code:
    #include <stdio.h>
    int palindrom(int num);
    int main()
    {
     int t;
     t=palindrom(541);
    return 0;
    }
    
    int palindrom(int num)
    {
    	int digit;
     int ans=0;
     int cnum,len=0;//length of the number
    //calculating the length of the array
    cnum=num;
     while (cnum/10)
    {
      digit=cnum%10;
      len++;
      cnum=cnum/10;
    }
     len++;
      return ans;
    }
    Last edited by newyork; 02-25-2011 at 10:39 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't see the algorithm to solve the problem, though. It's OK code, but no good for the purpose. I mean, you COULD peel off each digit, put it into a string, and then check if it's a palindrome, but why do that?

    Do you have itoa() on your system? if so, I'd use it to put the number into a string, and then check it using either a pair of pointers (left and right ends, coming together in the middle), or use the (imo easier to understand for beginners especially), the high and low index numbers, and bring them together toward the middle of the string, until they cross.

    If they meet, and a[left++] == a[right--], all the way through the string, then it's a palindrome.

    IMO, you need to work toward an algorithm first, and work on the details of the code, later. I'm used to working out programs solo, so that might very well just be my bias.
    Last edited by Adak; 02-25-2011 at 10:51 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. spelling checker
    By Tom.b in forum C Programming
    Replies: 8
    Last Post: 11-12-2009, 11:17 PM
  2. MISRA C Checker open source
    By huwan in forum Tech Board
    Replies: 8
    Last Post: 10-27-2009, 08:13 AM
  3. palindrom using recursion?
    By blackswan in forum C Programming
    Replies: 25
    Last Post: 04-29-2005, 05:28 PM
  4. Spell checker poem.
    By adrianxw in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-13-2004, 10:49 AM