Finding strings with consecutive characters in Java

user3003961

Write a function in Java which takes an Array of strings and from the array of strings returns only those strings which have a consecutive repetition of a particular letter for eg: if I/P is

{"Dauresselam", "slab", "fuss", "boolean", "clap"}

then O/P should be

{"Dauresselam", "fuss", "boolean"}

I could solve it using

import java.util.Scanner;
public class doubleChars {
    public static String[] getDoubles(String[]In)
    {

        int inLen=In.length;
        String zoom[]=new String[inLen];
        int count=0;
        if(inLen==0)
        {
            return zoom;
        }
        for(int i=0;i<=inLen-1;i++)
        {
            String A=In[i];
            //System.out.println(A);
            int striLen=A.length();
            for(int j=0;j<striLen-1;j++)
            {

                if(A.substring(j, j+1).equals(A.substring(j+1, j+2)))
                {
                    zoom[count]=A;
                    count++;
                    break;
                }
            }

        }
          return zoom;
        }
     public static void main(String[] args)
     {
         char more='y';
         int ab=0;
        String[] res={};
        String[] fillMe={"durres", "murres", "", "abcdeee", "boolean", "nger", "lagger"};
        Scanner strobe=new Scanner(System.in);
        System.out.println("Please enter the arraye of the string");
        /*while(strobe.hasNext())
        {
            fillMe[ab]=strobe.next();

            ab++;
        }
        */
        res=doubleChars.getDoubles(fillMe);
        for(int k=0;k<res.length;k++)
        {
            if(res[k]==null)
            {
                break;
            }
        System.out.println(res[k]);
        }
    }
}

IS there a way to use regex to make it shorter?

sp00m

You could use a backreference:

([a-z])\1

Regular expression visualization

Visualization by Debuggex


Java example:

String[] strings = { "Dauresselam", "slab", "fuss", "boolean", "clap" };

String regex = "([a-z])\\1";
Pattern pattern = Pattern.compile(regex);

for (String string : strings) {
    Matcher matcher = pattern.matcher(string);
    if (matcher.find()) {
        System.out.println(string);
    }
}

Prints:

Dauresselam
fuss
boolean

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding Consecutive Numbers in Java

From Dev

Finding consecutive characters in a 2D vector

From Dev

finding lenth of consecutive characters given an index

From Dev

Finding common characters in two strings

From Dev

finding how many consecutive numbers in an array [java]

From Dev

finding matching characters between two strings

From Dev

Finding text between two specific characters or strings

From Dev

Regular expression for finding more than 4 consecutive repeating characters in a string

From Dev

Characters "æ", "ø" "æ" in Java Strings (Windows)

From Dev

Restrict consecutive characters using Java Regex

From Dev

Spacing consecutive characters in string with Java regex

From Dev

How do I find intersections of consecutive characters in strings?

From Java

Is there an approach to finding the ASCII distance between two strings of 5 characters

From Dev

Finding all possible permutations of the characters in a set of strings using recursion

From Dev

finding characters and words in a strings without regex or nltk in python 3.5

From Dev

Finding combination of all strings in an array in Java

From Dev

Detecting Japanese characters in Java strings

From Dev

Combining regex of characters and Strings in JAVA

From Dev

Handling Strings with special characters in Java

From Dev

Finding minimum length of consecutive sub-series containing all distinct input strings

From Dev

Java, calculating difference between unique characters in strings

From Dev

Java operator overloading with strings+characters

From Dev

How to match strings having () or (any characters) java

From Dev

Java - Print strings showing newline characters

From Dev

Java, calculating difference between unique characters in strings

From Dev

Java - Print strings showing newline characters

From Dev

How to match strings having () or (any characters) java

From Dev

Finding the consecutive zeros in a numpy array

From Dev

finding largest consecutive region in table

Related Related

  1. 1

    Finding Consecutive Numbers in Java

  2. 2

    Finding consecutive characters in a 2D vector

  3. 3

    finding lenth of consecutive characters given an index

  4. 4

    Finding common characters in two strings

  5. 5

    finding how many consecutive numbers in an array [java]

  6. 6

    finding matching characters between two strings

  7. 7

    Finding text between two specific characters or strings

  8. 8

    Regular expression for finding more than 4 consecutive repeating characters in a string

  9. 9

    Characters "æ", "ø" "æ" in Java Strings (Windows)

  10. 10

    Restrict consecutive characters using Java Regex

  11. 11

    Spacing consecutive characters in string with Java regex

  12. 12

    How do I find intersections of consecutive characters in strings?

  13. 13

    Is there an approach to finding the ASCII distance between two strings of 5 characters

  14. 14

    Finding all possible permutations of the characters in a set of strings using recursion

  15. 15

    finding characters and words in a strings without regex or nltk in python 3.5

  16. 16

    Finding combination of all strings in an array in Java

  17. 17

    Detecting Japanese characters in Java strings

  18. 18

    Combining regex of characters and Strings in JAVA

  19. 19

    Handling Strings with special characters in Java

  20. 20

    Finding minimum length of consecutive sub-series containing all distinct input strings

  21. 21

    Java, calculating difference between unique characters in strings

  22. 22

    Java operator overloading with strings+characters

  23. 23

    How to match strings having () or (any characters) java

  24. 24

    Java - Print strings showing newline characters

  25. 25

    Java, calculating difference between unique characters in strings

  26. 26

    Java - Print strings showing newline characters

  27. 27

    How to match strings having () or (any characters) java

  28. 28

    Finding the consecutive zeros in a numpy array

  29. 29

    finding largest consecutive region in table

HotTag

Archive