Reverse of a string

Hi,

If I am executing following code after reversing it is giving "1" but I need "0001".

#include <stdio.h>

int main()
{
int n=1000, reverse = 0;


while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}

printf("Reverse of entered number is = %d\n", reverse);

return 0;
}

can anyone help on this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <algorithm>

int main()
{
	std::string myStr;
	int a;
	std::cout << "Enter a number: ";
	std::cin >> a;

	myStr = std::to_string(a);
	
	//reversing the string
	std::string strMy = std::string(myStr.rbegin(), myStr.rend());
	
	//as string has the leading zeroes!
	std::cout << "As string: " << strMy << '\n';	
	
	//but as an int, obviously, has no leading zeroes!
	int b = std::stoi(strMy);
	std::cout << "As integer: " << b << '\n';

	return 0;
}
Example 1
---------------
Enter a number: 1234
As string: 4321
As integer: 4321

Example 2
---------------
Enter a number: 1000
As string: 0001
As integer: 1
Last edited on
If you want to reverse a string, use a string, not an int.

see std::reverse
http://www.cplusplus.com/reference/algorithm/reverse/?kw=reverse

Or you can use std::ostream manipulators 'setfill' and 'setw' to add the 3 zeroes.
check out this:
http://ideone.com/f4Dk6a
In general, I'm not sure if it's possible to save number in variable with leading zeros.

when you surely know how many zeros precede the number, you can simply write
printf("%0Nd",var), where N is number of preceding zeros

when not, you can find out how many zeros are leading using while()

int zeros = 0; // zero counter
int d = 10; // variable used in modulo
while(number % d == 0)
{
zeros++;
d *= 10;
}

then do the reverse as you did, and at the end use for() to printf zeros we counted

for(int i = 0; i < zeros; i++)
{
printf("%d",0); // or you can print it as char using putchar()
}

now printf the reverse number
According to your requirement , this is the best way.........


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<conio>
#include<stdio>

void main()
{
   int i=0,temp;
   long num;
   char rev[10];
	cout<<"\n\nEnter Any Number - ";
   	cin>>num;
   while(num!=0)
   {
   	temp = num%10;
      rev[i] = temp + 48;
      num /= 10;
      i++;
   }
   rev[i]='\0';
   cout<<"\n\n\nReverse of the Number is - ";
   	puts(rev);
   getch();
}



Enter Any Number - 1000

Reverse of the Number is - 0001



Enter Any Number - 253

Reverse of the Number is - 352

@Hiten Sharma: If the number is too long, which is possible, you will get out of bounds errors on your array. Instead, use someting like a std::string. Alternately, just do this (works not only for numbers):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <algorithm>

int main() {
    std::string num;

    std::cout << "Enter a number: ";
    std::cin >> num;

    std::reverse(num.begin(), num.end());

    std::cout << "Reverse of number is: " << num << std::endl;

    return 0;
}


Some other little things:
Don't use void main. Also, don't use conio.h and its either stdio.h or cstdio.

EDIT:
Just saw @Thumpers post, this is the same thing.
Last edited on
Hi Hiten Sharma,

can you explain what is happening exactly in the following line which is highlighted?

while(num!=0)
{
temp = num%10;
rev[i] = temp + 48;
num /= 10;
i++;
}

In ascii the number 48 is the character '0'.
To store the number 2 for example in a char variable you have to look up the decimal-representation for the character '2' = 50. Now if temp is 2 for example and you add 48 ⇒ 50 ⇒ '2'.
Last edited on
Hi, look at this modification to your original code..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

int main()
{
   int n=1000, reverse = 0;
   
   int len =  (int)log10(n) + 1;
   
   while (n != 0)
   {
     reverse = reverse * 10;
     reverse = reverse + n%10;
     n = n/10;
   }

   len -= (int)log10(reverse) + 1;
   
   printf("Reverse of entered number is = ");
   for (int i =0; i<len; i++ ) printf("0");   
   printf("%d\n", reverse);
   return 0;
}
@cronopio

The code you provided is only applicable for the numbers which are perfect squares,cubes......so on.. of 10......What if the Number is something else like 1, 2001 or 589 ?
@Hiten_Sharma

say if n = 1 or n = 2001 or n = 589??
well, I would say you need a for loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
string whatever("Hello World!");

//display the string from end to beginning
for(string::const_iterator it = whatever.rbegin(); it != whatever.rend(); it++)
{
    cout<< *it;
}

//or...
{ //limit the scope of x...  you don't need to do this

    int x = whatever.size();
    do
    {
        x--;
        cout<< whatever[x];
    }
    while(x > 0);
}


If you have any questions about this, you should ask.
Topic archived. No new replies allowed.