Hi, I did not use any ArrayList/Stack. Hope this helpful.
import java.util.Scanner;
public class ReverseString {
private static Scanner input;
public static void reverse(String s)
{
String s1="";
for (int i=0; i<=s.length()-1; i++)
{
if(s.charAt(i)!=' ')
s1=s1+s.charAt(i);
else {
String ops=revst(s1);
System.out.print(ops+" ");
s1="";
}
if(i==s.length()-1){
String ops=revst(s1);
System.out.print(ops);
}
}
}
public static String revst(String s)
{
String revString = "";
for (int j=s.length()-1; j!=-1; j--)
revString= revString + s.charAt(j);
return revString;
}
public static void main(String[] args)
{
input = new Scanner(System.in);
System.out.println("Please enter a Sentence: ");
String ips = input.nextLine();
System.out.println("Sentence after reversing the words is:");
reverse(ips);
}
}