SvetlanPetrova

Replace Repeating Chars 1

Aug 9th, 2021
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.math.BigInteger;
  2. import java.util.Scanner;
  3.  
  4. public class Demo {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         String inputText = scanner.nextLine();
  8.         StringBuilder newText = new StringBuilder();
  9.         //1. добавям първата буква към новия текст
  10.         //2. взимам всички други букви до края
  11.         //-> проверявам дали текущата буква е различна от последната доабвена в новия текст
  12.         //ако е различна -> добавям я в новия текст
  13.         char firstLetter = inputText.charAt(0);
  14.         newText.append(firstLetter);
  15.         for (int index = 1; index < inputText.length(); index++) {
  16.             char currentSymbol = inputText.charAt(index);
  17.             char lastAddedSymbolInNewText = newText.charAt(newText.length() - 1);
  18.             if (currentSymbol != lastAddedSymbolInNewText) {
  19.                 newText.append(currentSymbol);
  20.             }
  21.         }
  22.  
  23.         System.out.println(newText);
  24.  
  25.  
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment