'Reversing the digits of a number in C++
I am trying to reverse the digits of a number. I used strings in it. And I am bound to use strings. Program just give last digit and stop executing. For instance if I put 123 as an input and I only get 3. Instead I should be having 321.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a,b=0;
cin>>a;
string str1="", str="";
for(int i=0;a>0;i++)
{
b=a%10;
str=to_string(b);
a=a/10;
str1=str1+str;
}
cout<<str1.length();
}
Solution 1:[1]
You're printing the string length rather than string itself.
Solution 2:[2]
You are Printing length of string .length() is a builtin function provided by strings. Try running it again by removing .length() keyword from cout command i.e. cout << str1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SomeWittyUsername |
| Solution 2 | Umar Tahir |
