This is a fairly simple problem: it just reverses a number or a string. For example, input "linroex" and the output is "xeornil", or input 123 and get 321. The solution isn't hard. In C++, you can access a string like an array, so we set up a loop whose end value is the length of the input string. On each pass, we move the last remaining character of string X to the first position of string Y. When the loop finishes, the reversal is complete.
Above, I explained string reversal. For number reversal, the basic idea is the same. The only difference is that at the end, you convert the reversed string array to an int, which removes any leading zeros.
What is number reversal?
It means reversing the digits of a number, and removing any leading zeros. For example, input 500 and you get 5, or input 5050 and you get 505.
Number reversal:
/*
Number reversal feature
For example: input 123 outputs 321
input 500 outputs 5 (the 0 is removed)
input 512 outputs 215
*/
#include
#include
using namespace std;
int main()
{
string x;
while(cin»x){
char re[100000]="";
for(int i=0;i<x.length();i++){ //Loop whose end value is the length of x
re[i]=x[x.length()-i-1]; //Copy the characters of array x into array re in reverse order: x is read from the back, and re is filled from the front
}
cout«atoi(re)«endl; //Convert the re array to an int and print it
}
return 0;
}
Code download: Box.net|4Shared|Dropbox|Google
To make it a string reversal instead, replace the line
cout<
with
cout<
and you get string reversal.
