waltermilner

Untitled

Dec 13th, 2014
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.57 KB | None | 0 0
  1.     String s = "1010110111111";
  2.     int oneCount=0;
  3.     // count the 1s
  4.     for (int pos=0; pos<s.length(); pos++)
  5.     {
  6.         if (s.charAt(pos)=='1') oneCount++;
  7.     }
  8.     int zeroCount=s.length()-oneCount; // assumes string is 1s and 0s only
  9.     // start result
  10.     StringBuilder sb=new StringBuilder();
  11.     // append 1s
  12.     for (int counter=0; counter<oneCount; counter++)
  13.         sb.append('1');
  14.     //append 0s
  15.     for (int counter=0; counter<zeroCount; counter++)
  16.         sb.append('0');
  17.     s=new String(sb); // back to String
  18.     System.out.println(s);
Advertisement
Add Comment
Please, Sign In to add comment