Home > Software design >  C reverse number with print and array
C reverse number with print and array

Time:10-29

A friend of mine programmed this in C:

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

int main(void) {
    int number;
    
    if (scanf("%d", &number) != 1) {
        printf("ERROR: While reading the 'int' value an error occurred!");
        return EXIT_FAILURE;
    }
    
    if (number == 0){                       //if number is 0 then nothing to do!
        printf("%d", number); 
    }
    else{
        char reverse[11];                   //created a char array of len 11
        
        int ind=0;                          //created int ind for array iteration!
        
        while(number)                       // while loop with the number
        {
            int digit = number % 10;        //calculate the digit that that will be in the first place in the char array

I want to know what this line in the code does:

            reverse[ind  ] = digit   '0';   //add digit to array at ind position

I know that it sets the digit in the beforehand created array at the position "ind" and then increment "ind 1" but I don't know what the '0' does.

            number = number / 10;           //cut the number so that we get the new number
        }
        
        reverse[ind]='\0';
        printf("%s\n",reverse);
    }
    return EXIT_SUCCESS;
}

CodePudding user response:

  • reverse is a character array (char[11])
  • All standard C functions use ASCII table to represent numbers as characters (each character has its own number), for characters '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' values are: 48, 49, 50, 51, 52...
  • Hence if you add 48 1 ('0' 1) you get 49 which corresponds to '1', So in general for decimal digits: '0' n = '<n>'.
  • For hexadecimal digits you can use: char digit = "0123456789ABCDEF"[n]

CodePudding user response:

char's are also just numbers.
Depending on the character set that's used each number gets assigned to a specific character.

You can take a look at the ASCII Code Table to see which numbers correspond to which character.

0-9 and A-Z and a-z are in order, which is very useful for mapping numbers.

So 0-9 would be:

Character Numeric Value
0 48
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57

So adding a number between 0-9 to '0' will result in the equivalent ascii character for that number, e.g:

assert('0' == '0'   0);
// ...
assert('5' == '0'   5);
// ...
assert('9' == '0'   9);

This conveniently also works for letters:

assert('A' == 'A'   0);
assert('J' == 'A'   9);
assert('Z' == 'A'   25);

The same could also be accomplished by just reversing the input, without parsing the number first:

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

int main(void) {
    char buf[256];
    if(!fgets(buf, sizeof(buf), stdin))
        return 0;
    for(int i = 0, len = strlen(buf); i < len / 2; i  ) {
        char tmp = buf[i];
        buf[i] = buf[len-1-i];
        buf[len-1-i] = tmp;
    }
    puts(buf);
    return 0;
}

If your standard library includes strrev, it's even shorter:

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

int main(void) {
    char buf[256];
    if(!fgets(buf, sizeof(buf), stdin))
        return 0;
    strrev(buf);
    puts(buf);
    return 0;
}
  • Related