Help with String algorithm

1

I need to make a program in which the user types a string and substring, and tells how many times this substring occurs in the main string. Ex: Main chain: "banana" Substring: "na" Repetitions: 2.

I've already thought about using a String vector, or the Substring command but I have not gotten anything so far.

The only attempt I could at least mount the code was:

    Scanner s = new Scanner (System.in);
    int controle, contador;
    contador = 0;
    String cadeia, palavra, substring;
    substring="";

    System.out.println("Insira uma frase e/ou palavras: ");
    cadeia = s.nextLine();

    System.out.println("Selecione uma palavra a ser verificada na cadeia: ");
    palavra = s.nextLine();
    char inicioSubstring = palavra.charAt(0);
    int fnalSubstring = (palavra.length()-1);
    char finalSubstring = palavra.charAt(fnalSubstring);
    int inicioSubs = -1;
    int fimSubs = -1;


    for (controle = 0; controle < cadeia.length(); controle++)
    {
        if (cadeia.charAt(controle) == inicioSubstring)
        {
            inicioSubs = controle;
        }
        if (cadeia.charAt(controle) == finalSubstring)
        {
            fimSubs = controle;
        }
        if(inicioSubs != -1 && fimSubs !=  -1)
        {
            substring = cadeia.substring(inicioSubs, fimSubs);
            if(substring.equalsIgnoreCase(palavra))
            {
                contador++;
            }   
        }   
    }
    System.out.println(contador);   
    
asked by anonymous 24.02.2018 / 14:27

2 answers

1

For the part of counting the total of times of a given String in the term you can use regular expression . For example:

String termo = "banana";
Pattern padrao = Pattern.compile("na");
Matcher combinacao = padrao.matcher(termo);

int contador = 0;
while (combinacao.find()) {
    contador += 1;
}

System.out.println(contador);
    
24.02.2018 / 14:41
2

Even without regular expressions you can do what you want with relative ease using the indexOf of String " and the overload that allows you to indicate only from a particular position.

To make it clearer I leave here only the declaration of the 2 methods in question:

  

public int indexOf(String str)

     

Returns the index within this string of the first occurrence of the specified substring.

E

  

public int indexOf(String str, int fromIndex)

     

Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

Implementation:

System.out.println("Insira uma frase e/ou palavras: ");
String cadeia = s.nextLine();

System.out.println("Selecione uma palavra a ser verificada na cadeia: ");
String palavra = s.nextLine();

int contador = 0;
int posicao = cadeia.indexOf(palavra);
while (posicao != -1){ //enquanto encontra a palavra
    contador++;
    posicao = cadeia.indexOf(palavra, posicao + palavra.length());
}

System.out.println(contador);

Output:

Insira uma frase e/ou palavras: 
banana
Selecione uma palavra a ser verificada na cadeia: 
na
2

View this code on Ideone

    
24.02.2018 / 20:39