Thread: program that reads in a five-digit integer and determines whether or not it is a pali

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    21

    Unhappy program that reads in a five-digit integer and determines whether or not it is a pali

    program that reads in a five-digit integer and determines whether or not it is a palindrome



    Code:
    Enter a five-digit number ( -1 to end ): 18181
    18181 is a palindrome
    
    Enter a five-digit number ( -1 to end ): 16738
    16738 is not a palindrome
    
    Enter a five-digit number ( -1 to end ): -1

    any one can help me on making this program?


    i have make it but dont quite get...

    this is my code that i have used

    Code:
    #include<stdio.h>
    #include<string.h>
    
    void main()
    {
    char strsrc[5];
    char strtmp[5];
    
    printf("\n Enter a five-digit number ( -1 to end ): "); gets(strsrc);
    
    strcpy(strtmp,strsrc);
    strrev(strtmp);
    
    if(strcmp(strsrc,strtmp)==0)
    printf("\n %s is a palindrome\n",strsrc);
    else
    printf("\n %s is not a palindrome\n",strsrc);
    }

    the program end after test 1 int, and -1 to end the program dont work

    can any one help?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. You can not fit a five-digit number into char strsrc[5]. The fact that gets allows you to get away with this is a failing of gets, which is why you shouldn't use that either.
    2. If you want repetition, you must use some sort of structure that provides it (a "loop" of some kind).
    3. Are you supposed to verify that they type a five-digit number and not "supercalifragilisticexpialidocious"?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    For a 5 digit number, you would need a string that is 6 chars long - or you have no space for the zero that indicates the end of the string.

    Are you sure that the actual test is supposed to read a string and reverse it to figure out if it's a palindromic number?

    You should NOT EVER use gets() to read user input, and particularly not for reading something into a small buffer of just a few chars. If someone enters 23023489034908239031092381092i839012 into your program, it would fall over in a big heap. It would be less objectionable if your buffer was 4KB long, but it's still borked. Use fgets().

    Or read in a number using scanf() (although that has it's own little problems), in which case comparing to -1 becomes a doddle.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    the out put should come out something like this

    Enter a five-digit number ( -1 to end ): 18181
    18181 is a palindrome

    Enter a five-digit number ( -1 to end ): 16738
    16738 is not a palindrome

    Enter a five-digit number ( -1 to end ): -1
    - this program just can accept 5 integer
    - can i use "int"?
    - how do i make a loop? do you mind show some here?
    - if using int how do i make it as revest? [strrev(strtmp);]

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by StormRoBoT View Post

    - this program just can accept 5 integer
    What?
    - can i use "int"?
    Certainly.
    - how do i make a loop?
    You read the section of your textbook that says "Loops". It's almost certainly at or before the section of your textbook that this problem came from.
    - if using int how do i make it as revest? [strrev(strtmp);]
    Usually, you use math to separate the digits, and then use math to reassemble them in a different order.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, you are currently lucky that your code is working right - it is not by design, but by luck. In the long term, you will find that trusting luck is not very reliable, and you should design your code to cope at the very least with the expected inputs [ideally also cope with unexpected input, e.g the "Elbow test", which is done by putting the elbow on the keyboard and making sure that it doesn't do anything real stupid].

    There are methods to revers a number as an integer. If you have a machine where integers are 32-bit, then that should be fine. Try "printf("%d\n", sizeof(int));" and see if it gives you 4. If it gives you 2, then you need "long int". (2 -> 16 bit -> max number is 32767, or if you use unsigned int it would be 65535).

    Have your teacher(s) not told you some way of producing loops?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    i still ot no idea on how to fix this.. sry very noob here... do you mind explane in detail..?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1. Read your number as an integer.
    2. Use some loop construct - there should be something in your handouts/book about how to do loops - there are several different choices.
    3. Find out how to use math to reverse the number.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    I would do this with math:

    If your number is 12321 how do you single out the first digit?
    How do you single out the last digit?

    After you have singled out the first and last digit and compared them how do you move on to the next digits to compare?

    Hint: the number 10, division, and modulus are important

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM