问题
I am looking for the C
program for reverse the digits like below:
If i enter:
123456
Then the result would be:
654321
Please help me.
回答1:
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.
回答2:
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);
}
}
回答3:
If your professor is grading you on performance, then try ggf31416's solution to this problem in another question:
int FastReverse(int num) {
int res = 0;
int q = (int)((214748365L * num) >> 31);
int rm = num - 10 * q;
num = q;
if (rm == 0) return -1;
res = res * 10 + rm;
while (num > 0) {
q = (int)((214748365L * num) >> 31);
rm = num - 10 * q;
num = q;
res = res * 10 + rm;
}
return res;
}
Optimization®
When it absolutely, positively has to be done this nanosecond.
回答4:
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;
}
回答5:
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
.
回答6:
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.
回答7:
Looks like a very old thread. But I referred the solutions here and had to cook up my own solution after testing the different programs available as solution on this website. I realized that treating the number as int will not yield necessary reversing of digit if the number ends with zero(s). So I decided to treat the number as a string and then reverse the digits. This way, I get exact reverse of the digit from a string standpoint and not a mathematical one that discards zeros at the beginning of a number.
#include <stdio.h>
#include <string.h>
main()
{
char num[20];
int x=0,slen;
printf("Enter the number you want to reverse :");
scanf("%s",num);
slen=strlen(num);
char *pointertoarray = &num[slen];
for (x=slen;x>=0; x--){ printf("%c",*pointertoarray); pointertoarray--; }
printf("\n");
}
回答8:
- count the number of digits in number and store in count variable
- extract individual digits in variable
use the power function.
while(count>=1) { var = num % 10; sum = sum + var * pow(10, count - 1); num =num / 10; count--; }
来源:https://stackoverflow.com/questions/2351251/c-program-for-the-reverse-the-digits