views:

488

answers:

6

I am looking for the c program for reverse the digits like ......

if i enter 123456

then the result would be .... 654321

plz any one help...

+4  A: 

Use % 10 to get the last digit. Output it. Divide the number by 10 to get all but the last digit. Use % 10 to get the last digit of that. And so on, until the number becomes 0.

sepp2k
as in `char buf[12]; snprintf(buf, 16, "% 10d", n); for ( size_t i = 10; i --> 0 ) printf("%c", buf[i]);`
Pete Kirkham
@Pete Kirkham: Cute and confusing what you did there. One thinks at first *wait, there's no "goes to" operator in C*.
P Daddy
i want the complete no this will generate the no 12345as54321but i want 54321compete
sandy101
+11  A: 

Here is a simple solution to this complex problem:

#include <stdio.h>

int main()
{
    int ch;
    ch = getchar();
    if (ch != '\n') {
        main();
        printf("%c", ch);
    }
}


A new version that outputs the newline:

#include <stdio.h>
#include <stdlib.h>

static void newline(void)
{
    printf("\n");
}

int main()
{
    int ch;
    ch = getchar();
    if (ch != '\n') {
        main();
        printf("%c", ch);
    } else {
        atexit(newline);
    }
}
Richard Pennington
+1 nice solution!
Tomas
I assume I was down voted because I didn't print the trailing newline? It was not a requirement. ;-)
Richard Pennington
But why not make it also print the newline? I'll +1 you if you do :P.
IVlad
Very nice, +1 :).
IVlad
+1 perfect answer to posted homework
Just a note: calling `main` recursively is not allowed in C++.
avakar
The question is tagged C, not C++.
Pete Kirkham
@Richard: Printing the final newline may be required by your C implementation. I don't have the standard but draft n1256 (C99 + TC1 + TC2) 7.19.2p2 says in part "Whether thelast line requires a terminating new-line character is implementation-defined."
Philip Potter
@Philip: That's interesting. I'd be pretty surprised if the standard didn't require that buffered output be flushed on program termination.
Richard Pennington
@Richard: If your implementation requires something of your program, and your program doesn't do it, then the behaviour is undefined, and so no, the standard doesn't require anything. I don't know of any systems which actually require the newline, but your program isn't strictly conforming unless it provides it. (Clearly, this is the most important criticism there is of your program :-)
Philip Potter
@Philip: See 7.20.4.3: The standard does require that streams be flushed. 7.19.2p2 just says it implementation defined whether the lack of trailing newline has any consequences. Not undefined, just implementation defined.
Richard Pennington
@Richard I think we're dangerously close to angels-on-pinheads here :)
Philip Potter
+1  A: 

Read the number in a char array A with scanf("%s", A) or, better, with fgets and output the char array reversed by outputting each character starting from strlen(A) - 1 to 0.

IVlad
A: 

strrev function reverses the string. If performance is not a problem then for instance you can do itoa, then strrev, then atoi. But the task is very simple even without strrev.

Oleg
If I'm not mistaken, strrev is not a standard function, and may not be available on all compilers.
IVlad
Yes, sure. In such case own implementation of strrev will help.
Oleg
Neither is `itoa()`...
Chinmay Kanchi
+6  A: 
P Daddy
+1 I like this much better than mine.
Richard Pennington
This hurts my eyes. Also upvote for optimizing something that hardly takes anytime on it's own.
Pim Jager
If the input is 120, what is the expected output? I think 021 or 21. Same for 1200. Is this algorithm producing correct results when the the input has trailing 0's?
ArunSaha
@ArunSaha: Given the application for the original algorithm, it does a fast exit when given a number with a trailing zero. And given that the expected output for an input with a trailing zero is not well defined here, I'd say that's still reasonable behavior. To remove this behavior, one must simply comment out the line `if (rm == 0) return -1;`. In this case, given 1200, it would return 21. Formatting with leading zeros would be the responsibility of a higher-order function.
P Daddy
A: 

Here is another possibility. It is non-recursive, and perhaps a little less code. There is an example in the code comments to explain the logic.

/*
    Example: input = 12345

    The following table shows the value of input and x
    at the end of iteration i (not in code) of while-loop.
    ----------------------
    i   |   input  |    x
    ----------------------
    0       12345       0
    1        1234       5
    2         123      54
    3          12     543
    4           1    5432
    5           0   54321
    ----------------------
*/
uint32_t
reverseIntegerDigits( uint32_t input )
{
    uint32_t x = 0;

    while( input )
    {
        x = 10 * x + ( input % 10 );
        input = input / 10;
    }

    return x;
}
ArunSaha