Reversing digits in C

This is a discussion on Reversing digits in C within the C Programming forums, part of the General Programming Boards category; Hello.. I wanted to know how can you reverse digits in "C" ? I am a Student of computer sciences ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    12

    Reversing digits in C

    Hello.. I wanted to know how can you reverse digits in "C" ? I am a Student of computer sciences and we are told about the "If" statements and the "Switch" statements and functions etc.. here's a small code

    Code:
    void main()
    {
    printf("Enter 4 digits");
    scanf("%d%d%d%d", &a,&b,&c,&d);
    ok after that I want something which will reverse the digits.. for example if a user inputs "2345" it should be reversed to "5432" in the output..

    many told me that it's not possible without loops and blah blah but how can they give us a assigment on something which they never taught us? so i'm sure there's a way.. please do not tell me exactly how to do it as i want to do it myself just give me a rough idea/hint .. thanks


    - DiGiTal ThouGhT!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    If there are a fixed number of digits, it is possible by manually printing them in reverse. Alternatively, you could use recursion, but that depends on whether recursion is included in the "blah blah".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Code:
    printf("%d%d%d%d", &d,&c,&b,&a);

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    First of all, main() should be declared as returning an int.

    A common way of doing it is to use basic math to get each digit... ie. division and modulus by 10, 100, etc. etc..

    Another way of reversing the digits is to not read it as an int, but to read it as a string, and then reverse it by going backwards char by char.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    12
    Quote Originally Posted by laserlight View Post
    If there are a fixed number of digits, it is possible by manually printing them in reverse. Alternatively, you could use recursion, but that depends on whether recursion is included in the "blah blah".
    what's recursion?

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    12
    Quote Originally Posted by Wiretron View Post
    Code:
    printf("%d%d%d%d", &d,&c,&b,&a);

    Already tried this, it did not work lol ;P

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    what's recursion?
    Recursion.

    On a more serious note, you could try searching the Web.

    Already tried this, it did not work lol ;P
    Wiretron made a typo (or four typos) and so the code prints the addresses, not the values. Try:
    Code:
    printf("%d%d%d%d", d, c, b, a);
    Note that this is what I mean by "manually printing them in reverse".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    12
    Quote Originally Posted by MacGyver View Post
    First of all, main() should be declared as returning an int.

    A common way of doing it is to use basic math to get each digit... ie. division and modulus by 10, 100, etc. etc..

    Another way of reversing the digits is to not read it as an int, but to read it as a string, and then reverse it by going backwards char by char.
    division and modulus by 10, 100 etc? can you give me an example? I didnt get it..

    and as far as strings are concerned im sure they did not teach us about strings etc yet so im sure they wont give us asigment off-topic lol :P

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    and as far as strings are concerned im sure they did not teach us about strings etc yet so im sure they wont give us asigment off-topic lol :P
    What have you been taught?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    12
    Quote Originally Posted by laserlight View Post
    Recursion.

    On a more serious note, you could try searching the Web.


    Wiretron made a typo (or four typos) and so the code prints the addresses, not the values. Try:
    Code:
    printf("%d%d%d%d", d, c, b, a);
    Note that this is what I mean by "manually printing them in reverse".

    I already tried both with and without address.. I knew that & will show the address and not the value, and I tried both with and without & yesterday before wiretron told me here =P and ok i'll search for it.. thnx

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    12
    Quote Originally Posted by laserlight View Post
    What have you been taught?
    functions .. "IF" statements.. "Switch" statements .. that's all..

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    I already tried both with and without address.. I knew that & will show the address and not the value, and I tried both with and without & yesterday before wiretron told me here =P
    I think I know why: you're reading them in in a way that you did not expect. 1234 is not 4 integers, but one integer.

    I suggest you change to use chars instead of ints, e.g.,
    Code:
    #include <stdio.h>
    
    int main()
    {
        char a, b, c, d;
        printf("Enter 4 digits: ");
        scanf("&#37;c%c%c%c", &a, &b, &c, &d);
        printf("In reverse: %c%c%c%c", d, c, b, a);
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    #include <stdio.h>
    
    int main()
    {
        int num = 1234;
        int i = 10;
           
        while(num)
        {
           printf("&#37;d", num % i);
           num /= i;
           i *= 10;
        }
           
       getchar();
       return 0;
    }
    ssharish

  14. #14
    Registered User
    Join Date
    Nov 2007
    Posts
    12
    Quote Originally Posted by laserlight View Post
    I think I know why: you're reading them in in a way that you did not expect. 1234 is not 4 integers, but one integer.

    I suggest you change to use chars instead of ints, e.g.,
    Code:
    #include <stdio.h>
    
    int main()
    {
        char a, b, c, d;
        printf("Enter 4 digits: ");
        scanf("%c%c%c%c", &a, &b, &c, &d);
        printf("In reverse: %c%c%c%c", d, c, b, a);
        return 0;
    }

    thanks alot, It worked.. but they did not tell us about return 0 as well as of yet so i modified it like the following

    Code:
    void main()
    {
        char a, b, c, d;
        clrscr();
        printf("Enter 4 digits: ");
        scanf("%c%c%c%c", &a, &b, &c, &d);
        printf("In reverse: %c%c%c%c", d, c, b, a);
        getch();
    }
    they also didnt teach us anything about libraries like include etc so thats why hehe.. anyway the idea was right about char hehe.. but it also works with "int" i chcked it but with int u have to give 1 by 1 .. like enter a digit then press "enter" then again another digit and press enter then it works.. with char it works like 1234 all at once.. so yah this was what i was lookin for, but i wonder why it dont work same way like char wid int? because 1234 shoud be inteegers


    anyway thanks alot.. cheers

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Say you had a number (integer) from the user: 180, 759.

    Now we begin to print it backwards, where un = user number:

    Code:
    /*this is a code idea - won't run as is */
    temp_un = un
    if temp_un > 0 && temp_un &#37; 10 != 0 {
       print temp_un % 10  - the left over 1's place value
       temp_un = temp_un / 10 - integer division, drops any value less than 1 to 0, perfect for this.
    }
    The normal way to do this would be to use a loop (perhaps a while loop, until temp_un was == 0).

    If you haven't had loops yet, you could just repeat this if statement, as many times as you needed to in order to cover the whole range of possible positive integers.

    1234567 can be just a bunch of char's in C, not a number. Just happens to look like one, but it's not a number. 1234567\0,
    with an end of string marker ('\0') elevates these bunch of char's into a single string.

    Still not a number, however, but strings can be handled very nicely in ways that numbers can not be (and vice-versa). Strings can be handled quite differently than char's, so the distinction is a very important one.

    In any programming, it's critical to know what data belongs to which data type!
    Last edited by Adak; 11-23-2007 at 11:34 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. decimal to number if digits in different bases
    By jorgejags in forum C Programming
    Replies: 21
    Last Post: 09-24-2008, 12:55 PM
  2. Hex digits stored in char array.
    By Kevinmun in forum C Programming
    Replies: 8
    Last Post: 11-18-2005, 03:05 PM
  3. Reversing Digits Project (Help Please)
    By fenixataris182 in forum C++ Programming
    Replies: 9
    Last Post: 07-12-2005, 01:08 PM
  4. Reversing the order of the digits?
    By Mak in forum C Programming
    Replies: 15
    Last Post: 10-09-2003, 09:17 PM
  5. reversing digits
    By lizardking3 in forum C++ Programming
    Replies: 15
    Last Post: 03-28-2003, 11:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21