Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How can I output the input backwards?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
RathcoM




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
corriep




PostPosted: 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
Dusk Eagle




PostPosted: 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:
Turing:

put "hello world" (5) %outputs 'o'


Off topic: 125th post!
RathcoM




PostPosted: 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
saltpro15




PostPosted: 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 Wink
Kharybdis




PostPosted: 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...

Turing:
var input := "54321"
for i: 1 .. length (input)
    put i ..
end for


here is what you really need ... figure out how it can apply to your code.

Turing:
var input := "This is a sentence!"
for decreasing i : length (input) .. 1
    put input(i) ..
end for


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
RathcoM




PostPosted: 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
tjmoore1993




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Dusk Eagle




PostPosted: 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.
jbking




PostPosted: 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.
DtY




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: