Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
number reversal
Author Message
Prince

Posted: 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?

Tony

Posted: 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
Tony's programming blog. DWITE - a programming contest.
Prince

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

how would i do that?
Tony

Posted: 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()
Tony's programming blog. DWITE - a programming contest.
Prince

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

riiigh... i dont get it
yuethomas

Posted: 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 ) 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 +5Bits - tony
Prince

Posted: 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 )
Asok

Posted: 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

Posted: 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

Posted: 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

Posted: 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

Posted: 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

Posted: 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
Catalyst

Posted: 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

Posted: 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
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 15 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: