How To Reverse A String .
This example explains how to reverse the string . Three different logic given
according to the requirement . I have printed one of them you can try it using
eclipse etc.
import java.util.StringTokenizer;
public class StringTokens {
public static void main(String[] args) {
// TODO Auto-generated method stub
String st = "This is string tokenizer";
System.out.println(st+"::");
System.out.println("After Reverse ");
//*********** Method NO1*****************
/*
StringBuffer bf =new StringBuffer(st);
StringBuffer stc =bf.reverse();
System.out.print("Reverse "+stc);*/
//************** Method No=2*********************************
StringTokenizer stz = new StringTokenizer(st);
int stcount=stz.countTokens();
int countst=0;
String[] stra=new String[stcount];
while (stz.hasMoreTokens()){
stra[countst]=stz.nextToken();
countst=countst+1;
}
for(int i=stra.length-1 ; i>=0 ;i--){
System.out.print(stra[i]);
System.out.print(";");
}
//************ Method NO 3 ***********
/*while(stz.hasMoreTokens()){
String ss=stz.nextToken();
for(int i=ss.length()-1 ; i>=0; i--){
System.out.print(ss.charAt(i));
}*/
}
}
Here is program output .
********************************
This is string tokenizer::
After Reverse
tokenizer;string;is;This;
How To Reverse A String .
Reviewed by Mukesh Jha
on
9:27 PM
Rating:
No comments:
Add your comment