5
I need to count the relative frequency of each letter in a string (only letters), without considering the spaces, without differentiating between upper and lower case and without differentiating accented letters. I managed to create a code that does more or less that, but the program repeats the counts unnecessarily. For example, in the phrase "Oh my cuckoo", the program prints "The letter appears 3 times" three times, when it should print only once. How do I fix this? And make the program count only the letters?
Solved!
public static void main(String[] args) {
Scanner ent = new Scanner(System.in);
String S;
int i, j, cont=0;
System.out.println("Digite a palavra/frase:");
// usuário digita string
S = ent.nextLine();
// a string é convertida para letras minúsculas
// para que não haja diferenciação entre 'A' e 'a'
String s = S.toLowerCase();
String v = "";
for (i=0; i<s.length(); i++) {
for (j=0; j<s.length(); j++) {
if (s.charAt(i)==s.charAt(j)) {
cont++;
}
}
// ao imprimir as frequências, exclui a contagem dos espaços
char c = s.charAt(i);
if (c >= 'a' && c <= 'z' && !v.contains("" + c)) {
v = v + c;
System.out.println("A letra "+s.charAt(i)+" aparece "+cont+" vezes.");
}
cont=0;
}
}
You can use the method
contains
classString
, or else the methodindexOf
.– Victor Stafusa
I chose to concatenate a new string, but I don’t know how to check if the new letter exists in this string. Use another FOR? Where should I put it?
– Cristiane Dos Santos Costa
@Cristianedossantoscosta I edited my answer. See if this solves your question. :)
– Victor Stafusa
I think it’s almost working... Which of the Ifs should I put your suggestion? I tried on the last one (I traded it for yours) and the program closes without printing anything.
– Cristiane Dos Santos Costa
@Cristianedossantoscosta It was on the last yes. Where you stated is String, what value you initialized it and how you are feeding it?
– Victor Stafusa
I changed the question code. See how it turned out.
– Cristiane Dos Santos Costa
Let’s go continue this discussion in chat.
– Cristiane Dos Santos Costa
No recommendation. the correct way to do this is with regular expression
– David Schrammel
@Davidschrammel I disagree with you. In this case regular expression is a cannon to kill an ant. It is an Overkill. In addition, regular expressions also perform well below that. Regular expressions are good for recognizing complex patterns and making substitutions with these patterns, not for counting occurrences. And finally, this is probably a programming exercise about basic string manipulation, and therefore regular expressions would be out of the context of the question. However, if you want to insist, post a response using regular expression and we’ll see.
– Victor Stafusa
@Victorstafusa there is no discussion, probably your code is faster, but when we put into play aspects like, legibility you lose, regular expressions were made specifically to solve problems with strings of a String,already its solution is a generic way not to say gambiarra.
– David Schrammel