Reverse words in a string

View previous topic View next topic Go down

Reverse words in a string

Post  Admin on Sat Aug 27, 2011 3:08 pm

public static void reverse(String str){
if(str==null||str.size==1)
return;
int i=0;
int j=str.size;
char temp;
char[] ch=str.toCharArray();
while(i<j){
temp=ch[i];
ch[i]=ch[j];
ch[j]=temp;
i++; j--;
}
return;
}

public void reverseWords(String str){
if(str==null)
return;
String revstr=str.reverse();
char[] rev=revstr.toCharArray();
String final="";
String temp="";
for(int i=0;i<revstr.length();i++){

if(rev[i]!=' '){
temp.append(rev[i++]);
}
else{
temp.reverse();
temp+=" ";
final.append(temp);
temp="";
}
}
return final.trim();
}

Admin
Admin

Posts : 131
Join date : 2011-08-16

View user profile http://codefornongeek.forumotion.com

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum