Results 1 to 18 of 18
- 04-07-2009, 12:03 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 16
- Rep Power
- 0
Reverse a string not using the substring method
hi , i need to implement a method that reverses a string but i can only use the method length, concant and charAt. I know how to do it with for loops or with the substring method , but im supposed to do it with recursion and im not allow to use the substring method.
Any help will be appreciated.
-
First thoughts:
A) what will the method return?
B) what will be the parameter passed to the method?
Edit: I don't know if this is the simplest way to do it, but here is a method signature that worked for me:
Java Code:public static String reverseString(String input, String output, int index) { //...Last edited by Fubarable; 04-07-2009 at 12:21 AM.
- 04-07-2009, 12:31 AM #3
Member
- Join Date
- Feb 2009
- Posts
- 16
- Rep Power
- 0
i have to use the method
public String reverse(String string) and i can use helper methods
- 04-07-2009, 12:46 AM #4
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
just use fubarable's method (or some variation of it) as a helper.
or something like that.Java Code:public String reverse(String string) { String retval = ""; return reverseString(string, retval, 0); }
- 04-07-2009, 05:31 AM #5
Member
- Join Date
- Feb 2009
- Posts
- 92
- Rep Power
- 0
conceptually:
string reverse(string){
if string contains 1 character
return string
else
return cancatonate(reverse(restof string), first element in string)
end reverse
My intro to programming used scheme (LISP variant), and we were about 6 weeks into the course before we stopped using strictly recursive techniques. My infinite all but dissappeared.
-
not bad, but "restof string" will need subString which the OP states cannot be used.
- 04-07-2009, 06:06 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
On a loop, get each character from the last index and append it into an empty string.
- 04-07-2009, 07:00 AM #8
Member
- Join Date
- Feb 2009
- Posts
- 22
- Rep Power
- 0
Java Code:private String reverse(String string) { String returnString =null; if(string.length() == 1) { returnString = string; } else { returnString = String.valueOf((string.charAt(string.length() -1))); returnString = returnString.concat(reverse(String.valueOf(string.subSequence(0, string.length() -1)))); } return returnString; }
- 04-07-2009, 12:24 PM #9
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
subSequence...
substring... , i think they functions the same... do not can use in the code or not
Eranga said
that recursive function may intake 2 parameters, String and intOn a loop, get each character from the last index and append it into an empty string.
int can indicate the starting pos(increment from 0 to length - 1) or ending pos (decrease from length - 1 to 0)
return char or string with 1 length, or what ever you like
- 04-07-2009, 12:29 PM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Something like this.
Ordering characters in reverse order. ;)Java Code:private String getReversString(String str) { String temp = ""; for(int i = 0; i < str.length(); i++) { try { temp = str.charAt(i) + temp; } catch(IndexOutOfBoundsException ex) { System.out.println("Index out of range exception found"); } } return temp; }
- 04-07-2009, 01:34 PM #11
Member
- Join Date
- Feb 2009
- Posts
- 22
- Rep Power
- 0
This is correct.
But his assignment is to bring up this without using
1 for loop
2 substring
and the method should be recursively called
- 04-07-2009, 01:38 PM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
On the original post only talking about the substring()
-
something like so:
called withJava Code:public static String reverseString(String input, String output, int index) { if (index == 0) { return output; } else { output += input.charAt(index - 1); return reverseString(input, output, index - 1); }
code not testedJava Code:String input = "Hello world!"; String reverseString = reverseString(input, "", input.length());
- 04-07-2009, 06:59 PM #14
Member
- Join Date
- Feb 2009
- Posts
- 92
- Rep Power
- 0
The original post did call for recursion, and was later clarified with a specific function signature.
emceenugget's method is IMHO, the best and correct method to solve the problem by adding a wrapper to call fubarable's method.
Pedagogical insights:
The use of helper methods.
That a recursive formulation of a problem can give easily understood insights regarding the solution, even if loops are a better (less memory, limited stack frame) solution in the long term. (just reverse the rest of the list and add the head to the end).
Let me also point out that passing the original string and an index is defining the "rest of the string", i.e., everything beyond the index. A substring method will work, so will passing an index (pointer) into the string.
I don't know how substring actually works, but a string variable is usually a pointer, and substring could be implemented by simply returning the pointer to the position in an existing string, rather than creating a new string. That is, passing a string and a pointer (index) is a simple implementation of substring. This would particularly be true in C. i.e. substring = string + ndx with its null terminated strings.
Don't want to be defensive, and I am, about any criticism of my previous post. fubarable understandably assumed an implementation I didn't state. Part of the scenery of forums, but I also think it is instructive to consider how these things work under the hood. Probably part of the point of the exercise.
Would be an interesting experiment which I may do later. If I declare String str = new String("abcdef") (and forgive any syntax errors) what is the value of foo = substring(ndx,str). Is the value of foo reasonably close to str , or way off in a distant area of the heap? Does foo increment in a constant way for substring(ndx + 1), str)?
So, my mind is racing trying to figure out the exact testing code, but, as I loved to see, that will be left as an exercise for the student.
- 04-07-2009, 07:22 PM #15
Member
- Join Date
- Feb 2009
- Posts
- 16
- Rep Power
- 0
i tested your code (Fubarable) and it works, but im supposed to use reverse(string) to call the method, and not
reverseString(input, "", input.length());
-
Sorry about making assumptions about code not posted. You are correct, and I have nothing really to add to your previous well-written post.Don't want to be defensive, and I am, about any criticism of my previous post. fubarable understandably assumed an implementation I didn't state. Part of the scenery of forums, but I also think it is instructive to consider how these things work under the hood. Probably part of the point of the exercise.
- 04-07-2009, 08:18 PM #17
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
once again, just encase fubarable's method within one that fits your requirements.
- 04-08-2009, 04:08 AM #18
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
how do i reverse this method for sorting?Again!
By PureAwesomeness in forum New To JavaReplies: 2Last Post: 03-09-2009, 12:51 AM -
[SOLVED] how do i reverse this method for sorting?
By PureAwesomeness in forum New To JavaReplies: 3Last Post: 03-08-2009, 09:37 PM -
Reverse and Replace a String in Linear Time
By colin.cruise in forum New To JavaReplies: 5Last Post: 07-01-2008, 09:02 PM -
How to use subString method
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:44 PM -
String substring function
By ravian in forum New To JavaReplies: 6Last Post: 01-02-2008, 07:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks