settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Series of Letters - Java Task (RegEx)

+13 votes

Write a program that reads a string from the console and replaces all series of consecutive identical letters with a single one.

Examples:

Input

Output

aaaaabbbbbcdddeeeedssaa

abcdedsa

 

asked in Java category by user john7

1 Answer

+1 vote
 
Best answer

Here is the solution:

import java.util.*;

public class Pr_03_SeriesOfLetters {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();

        input = input.replaceAll("([a-z])\\1+", "$1");
        System.out.println(input);
    }
}

You can read more about replacement string syntax here: http://manual.macromates.com/en/regular_expressions

answered by user Jolie Ann
edited by user golearnweb
...