Recursion Program/Code I'm having issues with...

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2005
Posts: 4
Reputation: CoolHandLuke is an unknown quantity at this point 
Solved Threads: 0
CoolHandLuke CoolHandLuke is offline Offline
Newbie Poster

Recursion Program/Code I'm having issues with...

 
0
  #1
Jun 15th, 2005
I am attempting to learn Java and am having a time of it. I am wanting to write a program that will allow a user to input number...then output them in reverse order. If someone would be so kind as to look at it and provide me with suggestions or hints...or for those who are extremely kind hearted feel free to tweak it for me and repost. On behalf of all those who receive assistance here, "Thanks in advance!" Code is as follows...and is attached (.java) as well.

//*****************************************
// revstring - recursively reverse a string
// Name:
// Date:
//*****************************************
import java.io.*;


public class RevString
{

//******************************************************
// main method reads a string input by the user and
// calls recursive methods that print string in
//reverse
// and return reversed string
//******************************************************
public static void main(String[] args) throws
IOException
{
InputStreamReader isr = new InputStreamReader
(System.in);
BufferedReader stdin = new BufferedReader (isr);

System.out.println("Enter a String");
String input = stdin.readLine();
System.out.print("The input in reverse is --> ");

rev(input);
System.out.println();

// String strrev = retrev(input);
// System.out.println();
// System.out.println("The returned string is --> " +
// strrev);

}
// ****************************************************
// recursive method that prints the reverse of a string
// public static void rev (string r)
{
String strRemaining = "1";
String str = String.valueOf(r);
int iLen = str.length(); //get length for
indexing substrings

if (iLen <= 1)
System.out.println(str);

else
{
//save the x-1 characters of string
strRemaining = str.substring(0, (iLen - 1));
//put the last character into substring
str = str.substring((iLen - 1), iLen);
//print last character
System.out.print(str);
//revert string back to number
r = Integer.valueOf(strRemaining).intValue();
//call itself recursively
Reverse(r);
}
//*****************************************************
// recursive method that returns the reverse of a string
// public static String retrev(String r)
// {
// }
}
}
Attached Files
File Type: java RevString.java (1.9 KB, 1 views)
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 21
Reputation: higherGround574 is an unknown quantity at this point 
Solved Threads: 1
higherGround574's Avatar
higherGround574 higherGround574 is offline Offline
Newbie Poster

Re: Recursion Program/Code I'm having issues with...

 
0
  #2
Jun 15th, 2005
the easiest way to break a number down right to left is to use the mod function
rightDigit = wholeNumber % 10 will give you the right most digit then all you have to do is subtract the rightDigit from the wholeNumber, divide it by 10 and pass the wholeNumber back into the parameter
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Recursion Program/Code I'm having issues with...

 
0
  #3
Jun 16th, 2005
You could always put it into a StringBuffer and reverse it:
  1. import java.util.*;
  2.  
  3. public class ReverseTest
  4. {
  5. public static void main(String[] args)
  6. {
  7. int reverseThisNumber = 12345678;
  8. StringBuffer sb = new StringBuffer(""+reverseThisNumber);
  9. System.out.println(sb.reverse().toString());
  10. }
  11. }

Or you could use a recursive String reverse method:
  1. public String reverse(String arg)
  2. {
  3.  
  4.  
  5.  
  6. String tmp = null;
  7. if (arg.length() == 1)
  8. {
  9. return arg;
  10. }
  11.  
  12. else
  13. {
  14.  
  15. //extract the last char
  16. String lastChar = arg.substring(arg.length()-1,arg.length());
  17.  
  18.  
  19. //extract the remaining chars
  20. String remainingString = arg.substring(0, arg.length() -1);
  21.  
  22. tmp = lastChar + reverse(remainingString);
  23. System.out.println(tmp);
  24. return tmp;
  25.  
  26. }
  27.  
  28. }

Then you could go with highergrounds idea and use modulus:
  1. public int revDigits (int num) {
  2. if (num < 10) {
  3. return num;
  4. } else {
  5. int s = num / 10;
  6. int d = num % 10;
  7.  
  8. int i = revDigits(s);
  9.  
  10. return (d * 10) + i; // incorrect
  11.  
  12. // need to find out how many digits in d:
  13. //return (d * 10^(num digits in d)) + i);
  14. }
  15. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4716 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC