
A Program To Reverse Words In String in Java
for example:
Input:- Computer Software
Output :- Software Computer
without using split() , StringTokenizer function or any extra ordinary fuction
only by using simple loop , if else , and for loop.
Please Tell Me This Program.

Here is an example that reverse the words in a sentence.
public class ReverseWordsUsingLoops{
public char[] reverseWords(char[] ch){
int i;
int index = 0;
for(i=0;i<=ch.length;i++){
if(i==ch.length){
reverse(ch,index,i-1);
}
else if(ch[i]==' '){
reverse(ch,index,i-1);
index = i+1;
}
}
reverse(ch,0,i-2);
return ch;
}
public char[] reverse(char[] ch,int start, int end){
char temp;
while(start<end){
temp = ch[start];
ch[start++]=ch[end];
ch[end--]=temp;
}
return ch;
}
public static void main(String args[]){
ReverseWordsUsingLoops rw = new ReverseWordsUsingLoops();
String s = "Conputer Software";
char[] ch = s.toCharArray();
System.out.println(rw.reverseWords(ch));
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.