Computer Science Canada How can I output the input backwards? |
Author: | RathcoM [ Wed May 27, 2009 4:24 pm ] |
Post subject: | How can I output the input backwards? |
I've been trying to figure it out for a while now, but lets say we input a number such as 54321, how could i manipulate that input to be output backwards (12345)? Thanks for any help. |
Author: | corriep [ Wed May 27, 2009 4:49 pm ] |
Post subject: | RE:How can I output the input backwards? |
Get the input as a string, use a for-loop to output it backwards |
Author: | Dusk Eagle [ Wed May 27, 2009 4:57 pm ] | ||
Post subject: | Re: How can I output the input backwards? | ||
To do as corriep said, you must realize that you can access individual characters in a string. For example:
Off topic: 125th post! |
Author: | RathcoM [ Wed May 27, 2009 5:08 pm ] |
Post subject: | RE:How can I output the input backwards? |
But i want it as an integer, not as a string as i am dealing with numbers |
Author: | saltpro15 [ Wed May 27, 2009 5:08 pm ] |
Post subject: | RE:How can I output the input backwards? |
decreasing for loop and a string and gratz Dusk Eagle! welcome to the user lounge! I'm going to write some nasty things about corriep since he can't see them |
Author: | Kharybdis [ Wed May 27, 2009 5:10 pm ] | ||||
Post subject: | Re: How can I output the input backwards? | ||||
as i'm sure that you don't need to do advanced string manipulation, for loops will do fine.. here is an example...
here is what you really need ... figure out how it can apply to your code.
its pretty hard to manipulate numbers with basic concepts ... that's why you have to transform them into strings. functions that apply to this: strint ; intstr ; strreal ; strintok |
Author: | RathcoM [ Wed May 27, 2009 5:31 pm ] |
Post subject: | RE:How can I output the input backwards? |
Thats the thing...i cant use fixed values for the variables, nor can i use strings as i am dealing with numbers. This is why it is a bit tricky |
Author: | tjmoore1993 [ Wed May 27, 2009 5:33 pm ] |
Post subject: | RE:How can I output the input backwards? |
Like everyone said, the best way is to convert to string. You might not be getting to basic concept as mentioned but I thought I should throw this in... Strings can be converted back into integers. |
Author: | Dusk Eagle [ Wed May 27, 2009 5:59 pm ] |
Post subject: | Re: How can I output the input backwards? |
The way I recommend you do this is to input your number as a string, and then check using strintok if the string can be converted to an integer before using strint to convert the string to an integer. The other way of doing it is to input the number as an integer and then convert it to a string using intstr. However, be aware that if the user enters a non-number, it will raise an exception. |
Author: | jbking [ Wed May 27, 2009 6:07 pm ] |
Post subject: | Re: How can I output the input backwards? |
If one wanted to avoid strings, here are a couple of ideas that I'll leave to you to code it as this is really being quite the help, IMO: To get the last digit of an integer, you could simply do a "mod 10" operation which will give you that last digit. You could build up the result by taking the previous result times 10 plus this last digit. I'm not sure if this solution would be preferred but it is kind of neat to my mind. |
Author: | DtY [ Wed May 27, 2009 6:09 pm ] |
Post subject: | Re: RE:How can I output the input backwards? |
RathcoM @ Wed May 27, 2009 5:31 pm wrote: Thats the thing...i cant use fixed values for the variables, nor can i use strings as i am dealing with numbers.
This is why it is a bit tricky You can't do that with numbers (as others have said, convert it to a string first.) The reason being that reversing the digits in an integer is pretty useless. |