Didart

Replace Repeating Chars

Nov 14th, 2021
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.76 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class ReplaceRepeatingChars {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String letter = scanner.nextLine().trim();
  8.  
  9.         StringBuilder sequence = new StringBuilder();
  10.  
  11.  
  12.         if (letter == null || letter.length() == 0) {
  13.             return;
  14.         }
  15.         char previousSymbol = letter.charAt(0);
  16.         sequence.append(previousSymbol);
  17.  
  18.         for (int index = 1; index < letter.length(); index++) {
  19.             char symbol = letter.charAt(index);
  20.             if (symbol != previousSymbol) {
  21.                 sequence.append(symbol);
  22.                 previousSymbol = symbol;
  23.             }
  24.         }
  25.         System.out.println(sequence);
  26.     }
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment