Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String s = "1010110111111";
- int oneCount=0;
- // count the 1s
- for (int pos=0; pos<s.length(); pos++)
- {
- if (s.charAt(pos)=='1') oneCount++;
- }
- int zeroCount=s.length()-oneCount; // assumes string is 1s and 0s only
- // start result
- StringBuilder sb=new StringBuilder();
- // append 1s
- for (int counter=0; counter<oneCount; counter++)
- sb.append('1');
- //append 0s
- for (int counter=0; counter<zeroCount; counter++)
- sb.append('0');
- s=new String(sb); // back to String
- System.out.println(s);
Advertisement
Add Comment
Please, Sign In to add comment