vendredi 4 mars 2016

How do you get the max value to be the return?

I have more or less finished my code, it works with most combinations of letters. It only fails if I enter something like "iiiaa." It returns that 'a' occurs the most, but something like "aaaii" returns that 'a' is also the most. I think the issue has to do with some sort of numerical value because the issue occurs if i list and letter 2 or more times followed by the letter 'a'. My code currently is:

  public static char most_frequent_character(String text)
{
int max_counter = 0;
char max_char = 'a';

for (int i = 1; i < text.length(); i++)
{
  char current = text.charAt(i);
  int counter = count_occurrences(text, current);

  char other_char = text.charAt(i-1);
  if (counter>count_occurrences(text, other_char))
  {
      max_char = current;

  }

}
return max_char;
}

count_occurrences returns the amount of times a letter appears in the word

Aucun commentaire:

Enregistrer un commentaire