// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.*;
class Main {
public static void main(String[] args) {
String s="hello world hi";
String[] str=s.split("\s+");
ArrayList<String> ar=new ArrayList<>();
for(String x:str){
StringBuilder sb=new StringBuilder(x);
ar.add(sb.reverse().toString());
}
StringBuilder sb=new StringBuilder();
int j=0;
int k=0;
boolean iswhite=false;
for(int i=0;i<s.length();i++){
if(Character.isLetter(s.charAt(i))){
if(iswhite)
j++;
sb.append(ar.get(j).charAt(k));
k++;
iswhite=false;
}else{
sb.append(" ");
iswhite=true;
k=0;
}
}
System.out.println(sb.toString());
}
// public static string reverse(String res){
// StringBuilder sb=new StringBuilder(res);
// return sb.revrese().toString();
// }
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter