0

Codechef isn't accepting the following code. Can anyone tell me what's wrong in it as I'm unable to point any mistake ?

//This program reverses a given integer.

#include<stdio.h>

int main(void)
{
    int t,n,l;

    scanf("%d",&t);

    while(t--){
        scanf("%d",&n);

        while(n>0){
          l=n%10;
          n=n/10;
          printf("%d",l);
        }

        printf("\n");
    }


    return 0;

}

t is no. of test cases.
n is the input integer.
l is some random variable to get the print job done.

This program is supposed to reverse a positive integer only.

Example:- Input - 1234   
          Output - 4321  
LPs
  • 16,045
  • 8
  • 30
  • 61
underdog_eagle
  • 25
  • 1
  • 1
  • 5
  • What is the purpose of your code? What is `t`? – Boiethios Jun 20 '16 at 08:50
  • By "reversing" I take it you mean printing it backwards with decimal notation? – Lundin Jun 20 '16 at 08:50
  • 1
    perhaps input `1000` output `1`, not `0001` – BLUEPIXY Jun 20 '16 at 09:00
  • 2
    The problem doesn't mention whether the reverse of "10" is "01", as in your code, or "1". The latter is more likely, as it's the result you would get if you really reversed an integer instead of printing the digits in reverse. – molbdnilo Jun 20 '16 at 09:01
  • 2
    [Reverse The Number](https://www.codechef.com/problems/FLOW007) – BLUEPIXY Jun 20 '16 at 09:02
  • Looking at the @BLUEPIXY link they (probably) want to see a `VLA` or `malloc`ated array for values. – LPs Jun 20 '16 at 09:07
  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, ***a specific problem or error*** [...]" – Jongware Jun 20 '16 at 09:42
  • @LPs What's "VLA"? No `malloc()` or arrays needed if one only uses arithmetics for this problem. Also, with the restrictions posed by the problem description, a string-based implementation can use a fixed-length buffer. – Kusalananda Jun 20 '16 at 10:23
  • @Kusalananda [**V**ariable **L**ength **A**rray](https://en.wikipedia.org/wiki/Variable-length_array). I wrote that due to the link where, it seems to me, user input all numbers in one session and output will be given in a second session. So you have to store input values somewhere and process them after the last value is stored. – LPs Jun 20 '16 at 10:29
  • @LPs There is nothing in the question that suggests a need to buffer the input. It only asks for certain output given certain input. – Kusalananda Jun 20 '16 at 10:31
  • @Kusalananda Last post, it's becaming too chatty. I guessed it because of in the linked page input are inputed sequentially and output are given sequentially after. The OP code looks good enough to be accepter, from my point of view, so I guessed something that cannot be clear. BTW probably BLUEPIXY pointed out the correct thibng to correct. Bye – LPs Jun 20 '16 at 11:45

2 Answers2

0

I got it. You must not print the non-significant '0'. For example 1230 gives 321. Here is my working code:

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

/* strrev reverses a given string */
char *strrev(char *str)
{
    char    tmp;
    size_t  i = 0,
            len = strlen(str);

    for (; i < len / 2 ; ++i)
    {
        tmp = str[i];
        str[i] = str[len - i - 1];
        str[len - i - 1] = tmp;
    }
    return str;
}

int main(void)
{
    char    buff[32],
            *s;
    size_t  i,
            len;

    scanf("%zu", &len);
    for (i = 0 ; i < len ; ++i)
    {
        scanf("%s", buff);
        s = strrev(buff);
        while (*s == '0')
            ++s; /* discarding '0' */
        printf("%s\n", s);
    }
    return EXIT_SUCCESS;
}
Boiethios
  • 38,438
  • 19
  • 134
  • 183
  • Nitpick: Since you're reversing the sting in-place, what's your reasoning to let `strrev()` return a pointer to the string? Apart from that: nice. – Kusalananda Jun 20 '16 at 09:29
  • @LPs It reverses an array of characters, which many would argue is simpler than doing arithmetics on an integer. – Kusalananda Jun 20 '16 at 09:31
  • Another nitpick: No need to read `%d`, just use `%s` and be rid of the conversion. – Kusalananda Jun 20 '16 at 09:32
  • @Kusalananda I changed the code, and it works. No need for string now. For your other question, the functions from `string.h` return the dest string. See `strcpy` for example. – Boiethios Jun 20 '16 at 09:32
  • @Kusalananda that is true :o But since I found that the trailing zeros must be discarded, I abandoned this method. – Boiethios Jun 20 '16 at 09:33
  • Ugh. Looks horrible in comparison to what you had... :-( Yes, that's good reasoning. – Kusalananda Jun 20 '16 at 09:34
  • Ok, I rewrite it :) – Boiethios Jun 20 '16 at 09:34
  • Your initial solution was beautiful. Use your generic `strrev()` routine, and then print the string _from the first non-zero digit_ (step through it with a `char *tmp`, then `printf("%s\n", tmp)`). – Kusalananda Jun 20 '16 at 09:37
  • @Kusalananda did it ;) – Boiethios Jun 20 '16 at 09:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115087/discussion-between-boiethios-and-kusalananda). – Boiethios Jun 20 '16 at 09:56
  • Correct format for `size_t` is `%zu`. Take a look [at this answer](http://stackoverflow.com/a/2524675/3436922) – LPs Jun 20 '16 at 11:42
0

try this code (I'm agree with molbdnilo's comment):

//This program reverses a given integer.

#include<stdio.h>
#include <math.h>

int main(void)
{
    int i,j,t,n,l,r = 0;
    char d[1024]; // stock digits

    scanf("%d",&t);

    while(t--){
        scanf("%d",&n);
        i = 0;
        while(n>0){
          l=n%10;
          n=n/10;
          d[i] = l;
          i++;
        }

        for(j=0;j<i;j++)
            r+= d[i-j-1] * pow(10,j); // r is the reversed number
        printf("%d\n",r);
    }


    return 0;

}
developer
  • 4,744
  • 7
  • 40
  • 55
  • Having `d` as `char []` is probably wrong, you meant `int []`. Also, since the question (on the external site) says numbers are less than or equal to 100000, the length of the array doesn't need to be 1024. – Kusalananda Jun 20 '16 at 10:28
  • Also, you need a space on that first `#include` line. – Kusalananda Jun 20 '16 at 10:33
  • 1
    @Kusalananda: no error for using char [] because digit does not execeed 1 byte, using space after #include is not mandatory – developer Jun 20 '16 at 10:51
  • That's true for the digit, but it's weird to use a `char` in a double precision arithmetic operation. It's also true that having no space between `#include` and the `<` or `"` is not mandatory, it will issue a warning with most compilers I've used. – Kusalananda Jun 20 '16 at 10:59