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

Username:   Password: 
 RegisterRegister   
 number reversal
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Prince




PostPosted: Thu Apr 03, 2003 9:48 pm   Post subject: number reversal

is there a way i can reverse a number and store it in a variable instead of just doing it digit by digit and outputting it to the screen?
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Thu Apr 03, 2003 9:56 pm   Post subject: (No subject)

what you mean reverse your number? like 123 to 321? if so, you gotta treat it as a string and reverse it that way.

intstr and strint
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Prince




PostPosted: Thu Apr 03, 2003 10:00 pm   Post subject: (No subject)

how would i do that?
Tony




PostPosted: Thu Apr 03, 2003 10:07 pm   Post subject: (No subject)

to reverse / flip a word

code:

for i:1..length(word)
put word(*-i +1)..
end for


to convert integer to string - intstr()
to convert string back to integer - strint()
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Prince




PostPosted: Thu Apr 03, 2003 10:15 pm   Post subject: (No subject)

riiigh... i dont get it Confused Embarassed
yuethomas




PostPosted: Thu Apr 03, 2003 10:47 pm   Post subject: (No subject)

Let's talk about string reversal first.

The idea is to put the last character of the original string as the first character of the resultant string, the second last character of the original string as the second character of the resultant string, and so on. So, supposing you need to flip a String (ugh, capitalised "String", Java habit Razz ) variable and put it in another String variable, you would do it like:

code:
var string1 : string := "abcdefg"
var string2 : string := ""

for decreasing i : length(string1) .. 1
    % so here, we're looping from the last character of string1 to the first.
    % note that we're storing the nth character of the
    % original string as the (length(string1)-n)th character of string2, so...
    string2 += string1 (i)
end for

% string2 should be "gfedcba" now.


Everything else should be self-explanatory. Convert your integer to a String, reverse it using the above algorithm, and convert the resultant String back into an integer... Ta-da.

Thx for explaining it better Smile +5Bits - tony
Prince




PostPosted: Tue Apr 08, 2003 10:27 am   Post subject: (No subject)

k now im having trouble converting from int to string and back...

code:

function reverseNumber (var n : int) : int
    var n2 : int
    n := intstr (n)
    for decreasing i : length (n) .. 1
        n2 += n (i)
    end for
    n2 := strint (n2)
    result n2
end reverseNumber


sumbody tell me y this isnt working... im about to giv up all hope on this stupid function (even tho it needs to be there Mad )
Asok




PostPosted: Tue Apr 08, 2003 10:33 am   Post subject: (No subject)

incorrect usage of intstr think about it like this intstr(n) = string. you're trying to assign that to an integer, which doesn't make any sence.
MysticAngel




PostPosted: Tue Apr 08, 2003 5:29 pm   Post subject: (No subject)

this is how i did it.
code:

var digits, n : int
var count : int := 0
put "The reverse order of the Number : "
loop
    digits := n mod 10
    n := n div 10
    count := count + 1
    put digits ..
    exit when n = 0
end loop
Prince




PostPosted: Tue Apr 08, 2003 6:03 pm   Post subject: (No subject)

yea i kno it would work like that but then how would i store it into a variable? thats wat im havin trouble with
Blade




PostPosted: Tue Apr 08, 2003 7:34 pm   Post subject: (No subject)

code:
var word:string:="word"
var wordint:int
wordint:=strint(word)
%do whatever you wanna do to it... then change it back
word:=intstr(wordint)


intstr converts integers to strings
strint converts strings to integers..
Prince




PostPosted: Tue Apr 08, 2003 9:35 pm   Post subject: (No subject)

ok dats all well and good wit words but say it started of like this:

code:

var num : int := 12345
var num2 : string
num2 := intstr (num)
%do whatever you wanna do to it... then change it back
num := strint (num2)


would that work?
Prince




PostPosted: Tue Apr 08, 2003 9:41 pm   Post subject: (No subject)

code:

function reverseNumber (var n : int) : int
    var n2 : string
    n2 := intstr (n)
    for decreasing i : length (n) .. 1
        x2 := n (i)
    end for
    n := strint (n2)
    result n2
end reverseNumber


would this work? im gonna kill this function if it doesnt start to work soon Evil or Very Mad
Catalyst




PostPosted: Tue Apr 08, 2003 9:46 pm   Post subject: (No subject)

code:

function reverseNumber ( n : int) :int
    var n2,hold : string:=""
    n2 := intstr (n)
    for decreasing i : length (n2) .. 1
        hold += n2 (i)
    end for
    result strint (hold)
end reverseNumber
Prince




PostPosted: Tue Apr 08, 2003 10:21 pm   Post subject: (No subject)

YES!!!!! thank u Catalyst... i knew u'd feel sorry for me sooner or later Very Happy
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  [ 15 Posts ]
Jump to:   


Style:  
Search: