How to reverse a String without using java.lang.String and java.lang.StringBuffer methods ?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Dhaneesh_P
Posted On:   Monday, January 21, 2008 11:48 PM

How to reverse a String without using java.lang.String and java.lang.StringBuffer methods ?

the question goes like this There should be a Class called StringReverse which had a constructor with the string to be reversed as parameter
			
ie. class StringReverse{
String str;
StringReverse(String str){
this.str=str;
}
public String reverse(){
//the code goes here
}
}

Re: How to reverse a String without using java.lang.String and java.lang.StringBuffer methods ?

Posted By:   Robert_Lybarger  
Posted On:   Tuesday, January 22, 2008 09:08 AM

That's a stupid question to begin with and poorly worded. Is it just the programmer that's not allowed to use those methods, or is nothing in the VM allowed to use those methods? (There are utility classes/methods that might be useful, but we all know they will call the same methods on the String instance anyway.) If we're going to play along in this silly game, then we need to know all the rules of what is and isn't allowed.

Re: How to reverse a String without using java.lang.String and java.lang.StringBuffer methods ?

Posted By:   Ajay_Ajay  
Posted On:   Tuesday, January 22, 2008 07:09 AM

Hey!! Try this... does this solve your purpose?


public class StringReverse{

String str;

StringReverse(String str){

this.str=str;

}

public String reverse(){

char[] ch = str.toCharArray();

char[] result = new char[ch.length];

int j=0;

int i=ch.length;

for(i=i-1;i>=0;i--)

result[j++] = ch[i];



return new String(result);

}




public static void main(String[] args) {

StringReverse strRev = new StringReverse("REVERSE");

System.out.println(strRev.reverse());

}

}

About | Sitemap | Contact