how to swap alternative words in a string

ex:
i/p:  this is the first good java prog
o/p: prog java  good first the is  this.
Radhika SinhaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
Please try this solution
 public static void main(String args[])
    {
        String inputstring = "this is the first good java prog"; /*Input string*/
        String[] splitter = inputstring.split(" "); /*Split the string using space*/
        String ReverseString = ""; /*Set the result reverse string to blank*/
        for (int looper = splitter.length - 1; looper >= 0; looper--) 
        {
          ReverseString += (splitter[looper] + " ");
        }
        System.out.println(ReverseString.trim()); /*Print the output - Reverse string*/    
    } 

Open in new window


OUTPUT
prog java good first the is this

Open in new window


ref1
or you can also refer this Ref-http://www.geeksforgeeks.org/reverse-words-in-a-given-string/
0
Radhika SinhaAuthor Commented:
thanks ...but can you do it without any builtin methods
0
Pawan KumarDatabase ExpertCommented:
Which built in methods you are talking about. Split?
0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Radhika SinhaAuthor Commented:
yes!!
split and trim both
0
Pawan KumarDatabase ExpertCommented:
We can do that but that will again take lot of efforts and will be slow as compared to functions provided by Java.
0
awking00Information Technology SpecialistCommented:
>>thanks ...but can you do it without any builtin methods<<
>>yes!!
 split and trim both
<<
It can't be done without using ANY built-in methods, but it can be done without using split and trim using indexes and substrings. What built-in methods can you use?
0
krakatoaCommented:
As an observation, your question is not going to be answered by anything that appears here in this thread, except by accident, since your question, which is, partly : "how to swap alternative words" would be wrong (even if it were right in the first place) according to the example output you require, because to swap "alternative" words, we would need you to tell us what those alternatives were so as to be able to swap them. On the other hand, if you meant "alternate" words, then each word would change position with its neighbour - another scenario with which your ambitions do not chime.

But as usual, badly worded questions (which put all the onus on unravelling their hidden meanings on the Experts), become bones of contention, over which endless arguments ensue, wasting everyone's time, usually for no ultimate reward, as they are invariably abandoned and bring the tone, mood and usefulness of the whole TA down.

So having said all that, here is a bit of code that you can put some more effort into, and improve to get rid of the ugly spatial insertion used as an artefact to avoid the pain of the better, alternative algorithm that is for you and others to discover.

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);
 }

}

Open in new window

0
krakatoaCommented:
I prefer this version now :

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");
 }

}

Open in new window

0
krakatoaCommented:
Here is a shorter version moving individual words :

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

0
krakatoaCommented:
And a version where everything is done within the same String :

class MoveBackWordsToFrontInSameString {

 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";
  int lastblank = s.lastIndexOf(" ");
  int tempblank = lastblank;
  int movingblank = 0;
  int filelen = 0;
  int len = s.length();

  while(filelen<len*2){
  
        if(tempblank == lastblank){s += s.substring(tempblank+1,s.length())+" ";}
        movingblank = (s.substring(0,lastblank-1)).lastIndexOf(" ");
        s += s.substring(movingblank+1,lastblank)+" ";
        lastblank = movingblank;
        filelen = s.length();
  }
  
  System.out.println(s.substring(len));

  
 }

}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
awking00Information Technology SpecialistCommented:
Radhika Sinha,
Given your example, it appears that your question is actually, "How can I create a new string that reverses the order of the words in a given string?" If that is what you really want, please respond to the experts and, if not, please respond with a more detailed explanation (perhaps with more examples) of what you need.
0
krakatoaCommented:
Answered by krakatoa in line with the  Asker's requirements.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.