class Backwards {
public static void main(String[] args){
String s = "this is the first good java prog";
String s1 = "";
String s2 = "";
for(int y=s.length()-1;y>-1;y--){
s1 += s.charAt(y);
}
s1 += " ";
int blankpointer = s1.indexOf(" ");
int downpointer = blankpointer-1;
while(s2.length()<s1.length()-1){
s2 += s1.charAt(downpointer);
if(s2.length()==blankpointer){
blankpointer = s1.indexOf(" ",blankpointer+1);
downpointer = blankpointer;
continue;
}
downpointer--;
}
System.out.println(s);
System.out.println(s1);
System.out.println(s2);
}
}
class Backwards {
public static void main(String[] args){
String s = "The Fall 3 Now the serpent was more crafty than any of the wild animals the Lord God had made. He said to the woman, Did God really say, You must not eat from any tree in the garden? 2 The woman said to the serpent, We may eat fruit from the trees in the garden, 3 but God did say, You must not eat fruit from the tree that is in the middle of the garden, and you must not touch it, or you will die. 4 You will not certainly die, the serpent said to the woman. 5 For God knows that when you eat from it your eyes will be opened, and you will be like God, knowing good and evil. 6 When the woman saw that the fruit of the tree was good for food and pleasing to the eye, and also desirable for gaining wisdom, she took some and ate it. She also gave some to her husband, who was with her, and he ate it. 7 Then the eyes of both of them were opened, and they realized they were naked; so they sewed fig leaves together and made coverings for themselves.";
//String s = "this is the first good java prog";
String s1 = "";
String s2 = "";
boolean blankcount = false;
for(int y=s.length()-1;y>-1;y--){
s1 += s.charAt(y);
}
int blankpointer = s1.indexOf(" ");
int downpointer = blankpointer-1;
while(!(s2.length() == s1.length()-1)){
s2 += s1.charAt(downpointer);
if(s2.length() == blankpointer){
blankpointer = s1.indexOf(" ",blankpointer+1);
if(blankcount){s2 = s2.substring(0,s1.lastIndexOf(" "))+" "+s.substring(0,s.indexOf(" "));break;}
if(blankpointer == s1.lastIndexOf(" ")){blankcount = true;}
downpointer = blankpointer;
continue;
}
downpointer--;
}
System.out.println(s+"\n");
System.out.println(s1+"\n");
System.out.println(s2+"\n");
}
}
class MoveBackWordsToFront {
public static void main(String[] args){
//String s = "The Fall 3 Now the serpent was more crafty than any of the wild animals the Lord God had made. He said to the woman, Did God really say, You must not eat from any tree in the garden? 2 The woman said to the serpent, We may eat fruit from the trees in the garden, 3 but God did say, You must not eat fruit from the tree that is in the middle of the garden, and you must not touch it, or you will die. 4 You will not certainly die, the serpent said to the woman. 5 For God knows that when you eat from it your eyes will be opened, and you will be like God, knowing good and evil. 6 When the woman saw that the fruit of the tree was good for food and pleasing to the eye, and also desirable for gaining wisdom, she took some and ate it. She also gave some to her husband, who was with her, and he ate it. 7 Then the eyes of both of them were opened, and they realized they were naked; so they sewed fig leaves together and made coverings for themselves.";
String s = "this is the first good java prog";
String s1 = "";
int lastblank = s.lastIndexOf(" ");
int tempblank = lastblank;
int movingblank = 0;
while(s1.length()<s.length()){
if(tempblank == lastblank){s1 += s.substring(tempblank+1,s.length())+" ";}
movingblank = (s.substring(0,lastblank-1)).lastIndexOf(" ");
s1 += s.substring(movingblank+1,lastblank)+" ";
lastblank = movingblank;
}
System.out.println(s1);
}
}
Open in new window
OUTPUT
Open in new window
ref1
or you can also refer this Ref-http://www.geeksforgeeks.org/reverse-words-in-a-given-string/