Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.61 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement