Code For NonGeek
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Reverse words in a string

Go down

Reverse words in a string Empty Reverse words in a string

Post  Admin 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

https://codefornongeek.forumotion.com

Back to top Go down

Back to top

- Similar topics

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