The Artima Developer Community
Sponsored Link

Java Answers Forum
Reverse a string with recursion and iteration

7 replies on 1 page. Most recent reply: Apr 20, 2003 6:36 PM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
Natalie

Posts: 1
Nickname: nat05
Registered: Mar, 2003

Reverse a string with recursion and iteration Posted: Mar 17, 2003 4:50 AM
Reply to this message Reply
Advertisement
Hi there,

I havent been doing java 4 long and am struggeling trying to write 2 methods:

1) which reverses a string using recursion
2) which reverses a string using iteration

I would be greatful for any help! Thanx xxxx


Rich Burgis

Posts: 17
Nickname: songbird
Registered: Mar, 2003

Re: Reverse a string with recursion and iteration Posted: Mar 17, 2003 6:40 PM
Reply to this message Reply
Try the following pseudo code

String reverse( String _s )
if length of _s == 0
return "";
else
return reverse( _s.substring( 1 ) ) + _s.charAt( 0 );

The iteration version is similar

Good luck
Rich

> Hi there,
>
> I havent been doing java 4 long and am struggeling trying
> to write 2 methods:
>
> 1) which reverses a string using recursion
> 2) which reverses a string using iteration
>
> I would be greatful for any help! Thanx xxxx

hien

Posts: 2
Nickname: hien
Registered: Apr, 2003

Re: Reverse a string with recursion and iteration Posted: Apr 17, 2003 2:11 PM
Reply to this message Reply
how do you reverse a string without using the substring predefined method... ???

hien

hien

Posts: 2
Nickname: hien
Registered: Apr, 2003

Re: Reverse a string with recursion and iteration Posted: Apr 17, 2003 2:37 PM
Reply to this message Reply
I have a class RString. I can't use the predefined methods except for charAt(),concatenation(+) and length(). I need to reverse a string, get the position of a particular character, check the two RStrings for equality(equals method(if teh same exact characters are in the same order adn return true if the Rstrings are identical, false otherwise. I also need to check to see if the string is a palindrome and be able to remove a character in the string. all this is in recursion. I only have so far iterations and kinda hard to do recursion. PLease help!

so my methods will be constructor: public RString(String s)
value = s;

public String toString()
return value;

public in indedOf(char c)

public boolean equals(RString other)

public removeAll(char c)

public reverse()

public boolean isPalindrome(String s)

Arun

Posts: 2
Nickname: lion
Registered: Apr, 2003

Re: Reverse a string with recursion and iteration Posted: Apr 18, 2003 10:05 AM
Reply to this message Reply
Here's a program which does it:

public String reverseString(String str)
{
char a=str.charAt( str.lastIndexOf(str) );
char[] ch=str.toCharArray();
char[] rev=new char[ ch.length];
for( int i=ch.length-1;i>=0;--i)
{
rev[(ch.length-1)-(i)] =ch;
}
return String.valueOf(rev);
}

Arun

Posts: 2
Nickname: lion
Registered: Apr, 2003

Re: Reverse a string with recursion and iteration Posted: Apr 18, 2003 11:47 AM
Reply to this message Reply
This seems to be a school assignment going by the restrictions placed.
Here is one primitive program which does your work thru recursion without using substring().
Take this as a template and modify it:

public String reverseString(String str)
{
if (str.equals(""))
return "";
else
{
char ch=str.charAt(0);
String substr= buildString(str);
//System.out.println(substr);
return reverseString(substr) +ch ;
}


}

public String buildString( String str)
{

char[] st=new char[str.length()];
for(int i=1;i<str.length();++i)
{
st=str.charAt(i);
}
return new String(st).trim();

}

FYI there is a very very easy way to reverse a string: using StringBuffer so you don't have to do all this circus

-Arun

Johnny Doe

Posts: 1
Nickname: panther
Registered: Apr, 2003

Re: Reverse a string with recursion and iteration Posted: Apr 19, 2003 10:28 PM
Reply to this message Reply
With the question that was asked before about the RString program. I believe this person is looking for a way to do this entire program without using the built in substring fucntion plus the .equals fucntion to compare strings. I know this, because I am also looking for the solution to this. How do we step through the characters in the string without using any for loops? For loops are restricted from us in this program also. Thanks for all your help.

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Here is reverse string by recursion... Posted: Apr 20, 2003 6:36 PM
Reply to this message Reply
Here is reverse string by recursion... It uses only charAt(), length() and + cancatenation ....
 
public class Test {
 
	public static void main (String[] args) throws Exception {
 
		String str = "abc";
		System.out.println ( str + ":" + reverseString ( str ) ) ;
 
		str = "Hello";
		System.out.println ( str + ":" + reverseString ( str ) ) ;
 
		str = "noon";
		System.out.println ( str + ":" + reverseString ( str ) ) ;
 
		str = "This is a bog string";
		System.out.println ( str + ":" + reverseString ( str ) ) ;
 
		str = "recursion";
		System.out.println ( str + ":" + reverseString ( str ) ) ;
 
 
 
	}
 
 
	public static String reverseString ( String source ) {
		if ( source == null ) {
			return null;
		}
 
		int len = source.length() ;
 
		if ( len == 0 || len == 1 ) {
			return source;
		}
 
		return reverseString ( source, source.length()) ;
	}
 
	public static String reverseString (String source, int position ) {
		if ( position <= 0 ) {
			return "";
		}
		else {
			String newStr = source.charAt( position - 1 ) + reverseString (source, --position);
			return newStr;
		}
 
	}
 
}

Flat View: This topic has 7 replies on 1 page
Topic: compiler error. cannot resolve symbol constructor Ex4_10(int) Previous Topic   Next Topic Topic: Java2 Clan Forums

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use