Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. // Task 1
  2. import java.util.Scanner;
  3.  
  4. public class Task1 {
  5.  
  6. static String result(int count, char c) {
  7. String s = "";
  8. if (count >= 4) {
  9. s += count +"@"+c;
  10. } else {
  11. for (int j = 0; j < count; j++) {
  12. s += c;
  13. }
  14. }
  15. return s;
  16. }
  17.  
  18. public static String stub(String s) {
  19.  
  20. String finalString = "";
  21. int count = 0;
  22. char c = s.charAt(0);
  23. char nextChar;
  24.  
  25. for ( int i=0; i<s.length(); i++) {
  26.  
  27. nextChar = s.charAt(i);
  28.  
  29. if(nextChar == c) {
  30. count++;
  31. } else {
  32. finalString += result(count, c);
  33. c = nextChar;
  34. count = 1;
  35. }
  36. }
  37.  
  38. finalString += result(count, c);
  39. return finalString;
  40. }
  41.  
  42. public static void main(String[] args) {
  43. Scanner sc = new Scanner(System.in);
  44. System.out.println( stub(sc.nextLine() ));
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement