Blogger

Delete comment from: Java67

Anonymous said...

//TODO: Reverse of String
public class ReverseString {
//Without using stringBuffer
public static void StringReverse(String str){
String rev="";
String rev2="";
String rev3="";
System.out.println("#########BY USING STRING CLASS##########\n");
/*TODO: Method 1 : a) Covert the String toCharArray it returns the array character..
* b) Then concatenate them
*/
System.out.println("######BY USING METHOD 1#######");
char chararra[]= str.toCharArray();
for(int i=chararra.length - 1;i>=0;i--){
rev= rev + chararra[i];
}

System.out.println("Result From method 1: "+rev);
/*TODO: Method 2 : a) Split The String into tokens and,
* b) Then concatenate them
*/
System.out.println("######BY USING METHOD 2#######");
String tokens[]= str.split("");
for(int i=tokens.length-1;i >= 0;i--){
rev2 = rev2+tokens[i];
}
System.out.println("Result From method 2: "+rev2);

/*TODO: Method 3 : a) calculate the length of string and use charAt(),
* b) Then concatenate them
*/
System.out.println("######BY USING METHOD 3#######");
int length= str.length();
for(int i=length-1;i>=0;i--){
rev3 = rev3+str.charAt(i);
}

System.out.println("Result From method 3: "+rev3);

}
//TODO: By Using StringBuffer..
public static void StringBuffer(String str){
StringBuffer stringbuff = new StringBuffer(str);
System.out.println();
System.out.println("########## BY USING STRINGBFFER CLASS#######");
System.out.println("\nReverse of String By Using reverse method: "+stringbuff.reverse());
}
}

Dec 5, 2015, 1:30:26 AM


Posted to How to Reverse String in Java with or without StringBuffer Example

Google apps
Main menu