Results 1 to 19 of 19
  1. #1
    kathyla18 is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default Reversing the characters within substring specified

    I need to implement two methods
    1) that reverses a string which i already done
    2) a method that will recursively take the String received, reverse the characters within substring specified, and return the new String containing the reversed subset (the non-reversed and reversed portions together).
    My code is below but for the second method i dont know how to return the string with the reverse subset, all i get is to return the string plus the reversed portion.


    XML Code:
    import java.util.Scanner;
    
    class recursionDemo{
    
    	public static void main (String[] args){
    	
    		Scanner scanner = new Scanner (System.in);
    		
    		System.out.println("Enter text to reverse: ");
    		String string = scanner.next();
    		System.out.println(reverse(string));
                    System.out.println("Enter a start");
                    int start = scanner.nextInt();
                    System.out.println("Enter an end");
                    int end = scanner.nextInt();
                    System.out.println(reverse(string, start, end));
    		
    
    	}
    	
                 public static String reverse(String string) {
                 
                    String output = "";
                    return reverse(string, output, string.length());
                }	
    	
                public static String reverse(String string, String output, int index){
                   if (index == 0){
                     return output;
                   }
                  else{
                 
                   output += string.charAt(index - 1);
                 
                   return reverse(string, output, index -1);
                  }
               }
     
               public static String reverse(String string, int start, int end) {
                 
                   if(start >= end) {
                     return string;
                   }
                   else {
                  
                     string += string.charAt(end - start);
                     return reverse(string, start, end-1);
                  }
               }
            
           
    
            
    }

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    just write two methods, reverse(String) and reverseSub(String,int,int).

    reverse() to reverse the whole string,
    and reverseSub to split the string, then call reverse().

    err... something like this:
    Java Code:
    String reverse(String s){
      // code
    }
    
    String reverseSub(String s,int n, int m){
      s = s.subString(...);
      return reverse(s);
    }
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    kathyla18 is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default

    i cant use the substring method i can only use charAt, length and concat

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you should have said that in the OP, I believe you had another post like this before?

    I don't think its possible if you're only allowed to use:
    -charAt
    -length
    -concat

    There must be some way to remove a character in every recursion.

    edit:
    are you allow to use "reverse(String string, int start, int end)".
    or only reverse(String) ?? which was stated in your other thread.
    Last edited by angryboy; 04-13-2009 at 09:44 PM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    kathyla18 is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default

    I should have both methods ...reverse(String string) and reverse(String string, int start, int end)

  6. #6
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    I'm not exactly sure what you want.

    I used reverse (String,String,int) instead to make
    Java Code:
    public String reverse(String orig) {
            return reverse(orig, "", orig.length());
        }
        public String reverse(String orig, String tmp, int index) {
            index--;
            // base case
            if(index < 0)
                return tmp;
            return reverse(orig, tmp.concat( "" + orig.charAt(index) ), index);
        }
    Is that what your trying to accomplish?

    Can you give use some sample input/output. ie what would
    Java Code:
    reverse("The quick brown fox jumps over the lazy dog");
    return?

  7. #7
    kathyla18 is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default

    the first method reverse(String string)
    reverse(java) returns avaj

    and for reverse(String string, int start, int end)
    reverse(java, 1, 3)
    returns jvaa

  8. #8
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Being limited simply to concat and charAt(int) really makes it overly complicated.

    I did it, however I'm not sure if this is the best method (or even if it will work properly). I didn't test it.

    I came up with
    Java Code:
        public String reverse(String orig) {
            return reverse(orig, 0, orig.length() - 1);
        }
        public String reverse(String buffer, int leftIndex, int rightIndex) {
            if(leftIndex >= rightIndex)
                return buffer;
            String tmp = "";
            for (int i = 0; i < buffer.length(); i++) {
                if(i == leftIndex) {
                    tmp = "" + buffer.charAt(rightIndex);
                    continue;
                }
                if(i == rightIndex) {
                    tmp = "" + buffer.charAt(leftIndex);
                    continue;
                }
                tmp = "" + buffer.charAt(i);
            }
            return reverse(tmp, ++leftIndex, --rightIndex);
        }
    Mr. Beans

  9. #9
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Quote Originally Posted by kathyla18 View Post
    I should have both methods ...reverse(String string) and reverse(String string, int start, int end)
    then I believe your problem had already been solved in the old thread.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  10. #10
    kathyla18 is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default

    It should only use recursion..... for loops are not allowed

  11. #11
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Well I guess you can make a method to recursively do what
    Java Code:
    for (int i = 0; i < buffer.length(); i++) {
                if(i == leftIndex) {
                    tmp = "" + buffer.charAt(rightIndex);
                    continue;
                }
                if(i == rightIndex) {
                    tmp = "" + buffer.charAt(leftIndex);
                    continue;
                }
                tmp = "" + buffer.charAt(i);
            }
    Does.

    Otherwise I'm not sure how to do it.

  12. #12
    kathyla18 is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by angryboy View Post
    then I believe your problem had already been solved in the old thread.
    On my last post they help me with the method reverse(string) and they used reverse(String string, String output, int index) as a helper method . But what i need is to also implement the method reverse(String string, int star, int end) and that's where im stuck.

  13. #13
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    it'd have to be somethign like this.

    Java Code:
    // start is inclusive,
    // end is exclusive.
    static String reverse(String string, int start, int end) {
    
      // error checking just for the heck of it.
      if(start<0||start>string.length()||
         end<start||end>string.length())
          throw new IllegalArgumentException();
    
      // return an empty string at the end.    
      if(start == end) return "";
    
      // calls itself recursively.
      return string.charAt(end-1) + 
        reverse(string, start, end-1);
    }
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  14. #14
    kathyla18 is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default

    If that method is used
    reverse(java, 1, 3)
    it returns vajava

    and what i need is for it to return jvaa

  15. #15
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    why would the reverse for "java" be "jvaa" ??!
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  16. #16
    kathyla18 is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default

    It is not the reverse of java. It is the reverse characters of the specified substring (1, 3) that's why its supposed to return jvaa( its reversing the a and v from java and keeping the non-reversed portion at the same position)

  17. #17
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    nm, ic you want to keep the original...(partly)
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  18. #18
    kathyla18 is offline Member
    Join Date
    Feb 2009
    Posts
    16
    Rep Power
    0

    Default

    yes, i need to keep the non-reversed and reversed parts together

  19. #19
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    The technique is a little different than what you did before. You still need a helper function.

    If you could use substrings the answer is simply substring (string 0 start) + reverse (substring(string start end)) + substring(string, end, str_len)



    Your helper function needs to take several different paths on the way to the base case.

    I may be doing too much of your homework for you, but you still have to figure out a few things to avoid off by one errors and to handle the base case correctly. The psuedo code has been coded in java and works as long as the base case is handled correctly. I am labeling each line because I don't know how this will look when posted

    1String helper_reverse(string, start, end, index, answer){
    2 until index matches start{
    3 answer = answer + the character pointed to by index + helper_reverse(string, start, end, ++index, answer);}
    4 when index matches start and is less than end {
    5 answer = answer + the character pointed to by calculate_reversing_ndx(ndx, start, end) + helper_reverse(...)}
    6 when index is greater than end{
    7 continue building the answer string as in line 3}
    8 return answer;
    } //end helper_reverse

    Let me repeat from a previous post that the concept of substring is being used. If you index into a string, you have three substrings: the string before the index, the string (character) at the index, and the string after the index.
    Last edited by rdtindsm; 04-14-2009 at 08:23 AM. Reason: add brackets in psuedo code, add return type

Similar Threads

  1. Reversing the String
    By Inaam in forum New To Java
    Replies: 1
    Last Post: 03-30-2009, 08:35 PM
  2. Reversing
    By whosadork in forum New To Java
    Replies: 14
    Last Post: 11-06-2008, 04:29 AM
  3. Reversing String
    By mew in forum New To Java
    Replies: 4
    Last Post: 12-02-2007, 09:42 PM
  4. reversing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 08:24 PM
  5. reversing a string
    By toad in forum New To Java
    Replies: 5
    Last Post: 11-07-2007, 09:13 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
  •