
-----------------------------------
Prince
Thu Apr 03, 2003 9:48 pm

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?

-----------------------------------
Tony
Thu Apr 03, 2003 9:56 pm


-----------------------------------
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

-----------------------------------
Prince
Thu Apr 03, 2003 10:00 pm


-----------------------------------
how would i do that?

-----------------------------------
Tony
Thu Apr 03, 2003 10:07 pm


-----------------------------------
to reverse / flip a word


for i:1..length(word)
put word(*-i +1)..
end for


to convert integer to string - intstr()
to convert string back to integer - strint()

-----------------------------------
Prince
Thu Apr 03, 2003 10:15 pm


-----------------------------------
riiigh... i dont get it :?  :oops:

-----------------------------------
yuethomas
Thu Apr 03, 2003 10:47 pm


-----------------------------------
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  :P ) variable and put it in another String variable, you would do it like:

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 :) +5Bits - tony

-----------------------------------
Prince
Tue Apr 08, 2003 10:27 am


-----------------------------------
k now im having trouble converting from int to string and back... 


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 :x )

-----------------------------------
Asok
Tue Apr 08, 2003 10:33 am


-----------------------------------
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
Tue Apr 08, 2003 5:29 pm


-----------------------------------
this is how i did it. 

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
Tue Apr 08, 2003 6:03 pm


-----------------------------------
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
Tue Apr 08, 2003 7:34 pm


-----------------------------------
 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
Tue Apr 08, 2003 9:35 pm


-----------------------------------
ok dats all well and good wit words but say it started of like this:


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
Tue Apr 08, 2003 9:41 pm


-----------------------------------

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:

-----------------------------------
Catalyst
Tue Apr 08, 2003 9:46 pm


-----------------------------------

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
Tue Apr 08, 2003 10:21 pm


-----------------------------------
YES!!!!! thank u Catalyst... i knew u'd feel sorry for me sooner or later :D
