SHARE
TWEET

Untitled

a guest Jun 20th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package forExam;
  2.  
  3. public class Exam {
  4.  
  5.     public static void main(String[] args) {
  6.         // TODO Auto-generated method stub
  7.         System.out.println(reverseString("  hello     world in java"));
  8.     }
  9.    
  10.     public static String reverseString(String str)
  11.     {
  12.         String res = "";
  13.         String tempstr = "";
  14.         for(int i = 0; i < str.length() ; i++)
  15.         {
  16.             if(str.charAt(i) == ' ')
  17.             {
  18.                 if(tempstr != ""){
  19.                     res = tempstr + " "+ res;
  20.                     tempstr = "";
  21.                 }
  22.             }
  23.             else
  24.             {
  25.                 tempstr = tempstr + str.charAt(i);
  26.                 if(i == str.length()-1)
  27.                 {
  28.                     res = tempstr + " "+ res;
  29.                 }
  30.             }
  31.         }
  32.         return res;
  33.     }
  34. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top