Invert a 3-digit number in C

Asked

Viewed 9,003 times

7

I need to make a program that the user type a three digit number and the program reverses that number for example 123 -> 321, I’d like to know the logic to do that, because I can’t think of anything.

4 answers

10


If you want to understand how the algorithm of a function that does the reversal works:

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

void reverse(char str[]) {
    int len = strlen(str) ;
    char tmp;
    for (int i = len / 2; i >= 0; i--) {
        tmp = str[i];
        str[i] = str[len - i - 1];
        str[len - i - 1] = tmp;
    }
}

int main(void) {
    char str[] = "123";
    reverse(str);
    printf("%s", str);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Doing it for math:

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

int reverse(int num) {
    int inv = 0;
    int dig = 0;
    for (int i = 2; i >= 0; i--) {
        dig = num / (int)pow(10.0, (double)i); //infelizmente C não tem uma função de potência para inteiro
        num -= dig * (int)pow(10.0, (double)i);
        inv += dig * (int)pow(10.0, (double)(2 - i));
    }
    return inv;
}

int main(void) {
    printf("%d", reverse(123));
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

8

If you need a function that reverses numbers of arbitrary size:

int inverte(int x) {
    int inv = 0;
    while (x > 0) {
        inv = 10 * inv + x % 10;
        x /= 10;
    }
    return inv;
}

What she does is take the rest of the number division by 10, that is, the right most digit of the number and add with the number we have until the moment times 10.

For example, if we reverse 23:

inv = 0
resto de 23 por 10 == 3
inv = 10 * inv + 3 == 3
dividimos 23 por 10 == 2
repetimos:
inv = 10 * inv + 2 % 10 == 10 * 3 + 2 == 32
  • I was doing it for calculation, I should have thought of using the module to avoid the power function.

  • Bring an Oscar for this myth! I was wondering how to do this with dynamic sizes. Your example was much simpler than I was thinking of doing, thank you very much Lucas, it helped a lot!

6

For the exact conditions of the problem, just this:

str[0] ^= str[2];
str[2] ^= str[0];
str[0] ^= str[2];

See working on IDEONE


Inline version :)

str[0] ^= str[2] ^= str[0] ^= str[2];

See working on IDEONE


Applying to strings of larger size:

int i, len = strlen( str );
for ( i = len / 2; i >= 0; i--)
    str[i] ^= str[len - i - 1] ^= str[i] ^= str[len - i - 1];

See working on IDEONE


I posted as "algorithmic curiosity", since the two solutions posted solve the problem well, in the traditional way.
To learn how this solution works, see this issue. (tip from @bfavaretto)

  • This is cool p/ who is more hardcore. Probably would have some benefits using like this.

-2

For this problem with 3 digits that is enough.

   #include <iostream>
   using namespace std;

   int main()
   {
    int n = 123;
    cout << "Valor inicial:" << n << endl;
    cout << "Valor Inverso:";
    cout << n % 100 % 10 / 1;
    cout << n % 100 / 10;
    cout << n / 100;
   }
  • 2

    Why on that line cout << n % 100 % 10 / 1; you divide by 1 ? See https://ideone.com/SqpLvD

  • 2

    Along the same lines, n % 100 % 10 is equivalent to n % 10, It is not necessary to first take the rest of the division by 100 to, then take the rest of the division by 10. Taking advantage, this question is about [tag:c] and your answer is in [tag:C++], I believe that making explicit in the text of the answer that you are giving the example in another language can help those who arrive here in the future not to get confused

Browser other questions tagged

You are not signed in. Login or sign up in order to post.