Thursday, December 3, 2015

Java Code To Check Repeated Character In String

import java.util.regex.Pattern;
import java.util.regex.Matcher;

class RepeatedCharacterCheck {
    public static void main (String[] args) throws java.lang.Exception {
        String stringToMatch = "abccdeeefenonn";
        Pattern p = Pattern.compile("(\\w)\\1+");
        Matcher m = p.matcher(stringToMatch);
        while (m.find()) {
            System.out.println("Duplicate character " + m.group(0));
        }
    }
}

Output

Duplicate character cc
Duplicate character eee
Duplicate character nn

No comments:

Post a Comment