Results 1 to 6 of 6
  1. #1
    atinsuy is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default Recursion in java

    need helps to solve the following problem:

    Write a recursive method that takes a sentence and returns the sentence in reverse order. For example, given “the cat is brown” as input, the program should print out “brown is cat the”. :confused:

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    write a blank method to take your string and reverse it
    Java Code:
    String theString = "The cat is brown";
    
    public String reverseTheString(String theString) {
        //method body
    }

    now try to work out the logic to do what you want

    Java Code:
    {
    //method body
    
    //create and initialise a new String to hold the reversed String
    String theStringReversed = "";
    
    int length = theString.length();
    //make a loop using the length
    while (length > 0) {
    
    length--
    }
    
    }

    I think the rest is fairly simple... copy from a to be inside the loop

    Java Code:
    theStringReversed += theString.charAt(length);

    and don't forget to set your return statement:
    Java Code:
    return theStringReversed;

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    And sorry, I just reliased you don't need the string backwards, but you need each sub-string backwards. And since you need to write a 'recursive' method I doubt you're allowed to use String.split.


    So first, you need to find each whitepace and make a new String

    1. Loop throught the string
    Java Code:
    while (length > 0) {
    
    length--;
    }
    2. In each iteration, check for a white space
    Java Code:
    if (string.charAt(length).equals(" ")) {
    
    } else {
    
    }
    3.A) Determine what to do if you find a whitespace
    Java Code:
    //increment counter to assign the next lot of characters to a new String
    stringCount++;
    3.B) Determine what to do if you don't find a whitespace
    Java Code:
    //add character to current string
    myStrings[currentString] += c;
    4. from the above we can determine that we will need to have created a new String array to hold each substring
    Java Code:
    //can hold a maximum of 20 strings
    myStrings[] = new String[20];
    5. declare a counter to tell the program which string we are writing to
    Java Code:
    //hold the index of the current string from the array
    int stringCount = 0;
    6. going back to the loop, we need to iterate through the characters of the whole sentence
    Java Code:
    char c = string.charAt(length);

  4. #4
    atinsuy is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    hi ozzyman

    Thank you for your response...but i'm still blur...

    here my codes

    import java.util.Scanner;
    /**
    * Write a java class named reverseWords using recursion.
    * It takes a sentence and returns the sentence in reverse order.
    * For example, given "the cat is brown" as input,
    * your program should print out "brown is cat the".
    * Note that space is the only separator.
    */

    public class reverse
    {
    public static String reverseWords(String s)
    {
    if(s == null || s.isEmpty() || !s.contains(" ")) //base case
    return s;
    else ???
    return ?????? //what's the suitable general case????

    }

    public static void main(String[]args)
    {
    Scanner sc = new Scanner(System.in);
    System.out.println("type your string: ");
    String x = sc.nextLine();
    System.out.println(reverseWords(x));
    }
    }

    help me...:-(

  5. #5
    Andrew_2 is offline Member
    Join Date
    Mar 2011
    Posts
    36
    Rep Power
    0

    Default

    you want it as recursion so why you want to use while loop !

    be attention any time when you want to deal with the String Recursion
    Remeber these three this :

    1. substring
    2. CharAt(int i)
    3 s.length()

    the method will work like that

    take String as parameter
    public ................. ( String s )

    then

    check whether string length if zero if it is .. return no thing
    then print charAt(0)
    then recursive case : call function by substring one char.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Was just going to bring that up, you don't want to use a loop in recursion. Recursion means you should be making recursive calls. There should also be a termination condition. The basic recursive method should look something like this
    Java Code:
    public int recursiveMethod(int n){
      if(termination condition){
        return answer;
      }
      else{
        recursiveMethod(make some change to arg);
      }
    }
    Here is a factorial done recursively.

    Java Code:
    public int factorial(int n){
      if(n == 1){ //termination condition
        return 1;
      }
      else{
        return n * factorial(n - 1);  //recursive call
      }
    }
    With this method you want to work with the use of substring is going to be incredibly helpful. As will last index of, this method will be invaluable.

    Java Code:
     int 	lastIndexOf(int ch)
              Returns the index within this string of the last occurrence of the specified character

    Give it a shot with those methods in mind and see what happens. Remember, there needs to be a termination condition, and then a recursive call which does something and changes the size of the argument.

Similar Threads

  1. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 07:26 PM
  2. java recursion help
    By khdani in forum New To Java
    Replies: 4
    Last Post: 12-29-2008, 09:56 PM
  3. Recursion in Java ..
    By Java01 in forum New To Java
    Replies: 6
    Last Post: 10-24-2008, 12:42 PM
  4. i could not get the recursion in java
    By sivasayanth in forum New To Java
    Replies: 3
    Last Post: 04-23-2008, 09:08 AM
  5. Recursion in java
    By lenny in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 07:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •