Help with String algorithm

Asked

Viewed 91 times

1

I need to make a program in which the user types a string and a subletter, and inform how often this subletter occurs in the main string. Ex: Main chain: "banana" Subdivision: "na" Repetitions: 2.

I’ve thought about using a String array, or even the Substring command but I haven’t got anything so far.

The only attempt I was able to at least assemble 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);   
  • And what have you tried to do? Demonstrate that you’ve had some effort trying something and add to your question.

2 answers

2

Even without regular expressions you can do what you want with relative ease using the method indexOf of String and the Overload indicating only from a given position.

To make things clearer, I would just like to mention the two methods in question:

public int indexOf(String str)

Returns the index Within this string of the first Occurrence of the specified substring.

And

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);

Exit:

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

See this code in Ideone

1


For the part of counting the total 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.