Reversing the digits
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Member
Join Date
Feb 2012
Location
INDIA
Posts
25

Reversing the digits

//PROGRAM TO REVERSE THE 5 DIGIT NUMBER
#include <stdio.h>
void main()
{

int num, i = 0;

char rev[5];

printf("Enter any 5 digit number\n");

scanf("%d", &num);
i = 0;
while(num)
{

rev[i] = num % 10;// IS THIS RIGHT WAY TO ASSIGN CHARACTER ARRAY?????

i++;

num = num/10;
}

printf("%s", rev);

}

What's wrong in the above program.

WHILE DISPLAYING i am not getting correct output. whats wrong in rev[] array.

2. Elite Member
Join Date
May 2009
Posts
2,413

Re: Reversing the digits

Originally Posted by Tanushreeagr
rev[i] = num % 10;// IS THIS RIGHT WAY TO ASSIGN CHARACTER ARRAY?????
You're extracting the rightmost digit from the integer held in num. It will be an integer between 0 and 9. What you want to do now is to convert it to a char between '0' and '9' before assigning it to the array. Since those characters lie next to each other in the ASCII encoding this will work (although it isn't that elegant)

rev[i] = '0' + num % 10;

http://www.asciitable.com/

The ASCII char '0' is represented by 48 which is added to the digit between 0 and 9 turning it into an ASCII char between '0' and '9'.
Last edited by nuzzle; March 9th, 2013 at 01:41 AM.

3. Re: Reversing the digits

If you just want to reverse an integer, see this thread

http://forums.codeguru.com/showthrea...ighlight=emirp

Code:
```//Reverse an integer
int ReverseInteger(int no)
{
int rnum = 0;

while (no) {
rnum = rnum * 10 + (no % 10);
no /= 10;
}

return rnum;
}```

4. Re: Reversing the digits

PS. When you post code, please use CODE tags for readability. Go Advanced, select code then click '#'

5. Re: Reversing the digits

Originally Posted by 2kaud
If you just want to reverse an integer, see this thread

http://forums.codeguru.com/showthrea...ighlight=emirp

Code:
```//Reverse an integer
int ReverseInteger(int no)
{
int rnum = 0;

while (no) {
rnum = rnum * 10 + (no % 10);
no /= 10;
}

return rnum;
}```
I considered that approach as well, but whether it actually is valid depends on the exact requirements set up by the assignment: It behaves different from the char[]-based approach when the subject number ends with one or more zeroes. These would become insignificant in processing and thus omitted in the output. That was irrelevant in the emirp context since there are no primes ending with 0 anyway.

6. Re: Reversing the digits

Here are some fixes to your code. Study it and my comments.

Originally Posted by Tanushreeagr
Code:
```//PROGRAM TO REVERSE THE 5 DIGIT NUMBER
#include <stdio.h>
void main()
{

int num,  i;  // Initializing i here is not needed since it is done later.

char rev[6]; // You need to allocate space for the null character to terminate the string.
// This is required by the printf() function.

printf("Enter any 5 digit number\n");

scanf("%d", &num);
i  =  0;
while(num & (i < 5)) // It is very important that you check that the number is not too long.
// Without this check data could be written outside of the rev array causing memory corruption.
{

rev[i]  =  '0' + (num % 10);

i++;

num  =  num/10;
}
rev[i] = '\0';  // Make sure you terminate the string with a null character.

printf("%s\n", rev);   // This prints a new line after the result.

}```

7. Re: Reversing the digits

To make the revised program work, the while condition test should be the logical and not the bitwise and.

Code:
`while(num && (i < 5))`
Also it would be good practice to declare and initialise i as close as possible to where it is first used (or at least initialise it where it is defined).

main() should return an integer by the ANSI standard.

Code:
```//PROGRAM TO REVERSE THE 5 DIGIT NUMBER
#include <stdio.h>

int main()
{
char	rev[6];		// You should allocate space for a null character to terminate the string.

int	num;

printf("Enter any 5 digit number: ");
scanf("%d", &num);

int	i = 0;

while (num && (i < 5))	// It is very important that you check that the number is not too long.
// Without this check data could be written outside of the rev array causing memory corruption.
{
rev[i++]  =  '0' + (num % 10);
num  /=  10;
}
rev[i] = '\0';		// Make sure you terminate the string with a null character.

printf("%s\n", rev);	// This prints a new line after the result.
return (0);
}```

8. Re: Reversing the digits

Originally Posted by 2kaud
To make the revised program work, the while condition test should be the logical and not the bitwise and.
Doh!

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•