title: Splitting a sentence into words using for loop or while loop in java
body: <p>i have to do a program which takes a sentence and reverses it word by word in java. for eg:
  India is my country</p>
  
  <p>output:aidnI si ym yrtnuoc</p>
  
  <p>ive figured out all of it but i just cant split a sentence into separate words.im not allowed to use split function but im meant to use either substring or indexof();while loop and for loop are allowed.
  this is what ive got so far:</p>
  
  <p>import java.io.*;</p>
  
  <p>public class Rereprogram10</p>
  
  <p>{</p>
  
  <pre><code>public void d()throws IOException
  
  {
  
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  
   String str;
  
   System.out.println("input a string");
  
  
   str=br.readLine();
  
   String rev="";
   int length=str.length();
   int counter=length;
   for(int i=0;i&lt;length;i++)
   {
       rev=rev+str.charAt(counter-1);
       counter--;
      }
      System.out.println("the result is: "+rev);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>its wrong though,the output keeps on coming:
  yrtnuoc ym si aidnI
  i havent learnt arrays yet...</p>