метод, который заменяет большее вхождение новым символом? - PullRequest
0 голосов
/ 14 июня 2019

Упражнение: создайте метод, который подсчитывает вхождения и также принимает символ в качестве входных данных. Символ с наибольшим вхождением заменяется символом, вводящим метод (новый символ)

public void myMethod() {

    String text = "ovolollo";

    int numberOfLetterA = ProvaEsercizio9.countCharOccurrences(text, 'a');
    ecc..

    System.out.println("Lettera a = " + numberOfLetterA);   
    ecc..   
}


public static int countCharOccurrences(String source, char target) {
    int counter = 0;

    for (int i = 0; i < source.length(); i++) {
        if (source.charAt(i) == target) {       
            counter++;
        }
    }
    return counter;
}

Ответы [ 3 ]

0 голосов
/ 14 июня 2019

Вы можете сделать что-то вроде этого:

private static String replaceMostFrequentChar(String text, char withChar) {
    // count the number of occurrences of each character in `text`
    Map<Character,Integer> counts = new HashMap<>();
    for (char c: text.toCharArray()) {
        Integer count = counts.get(c);
        if (count == null) {
            count = 1;
        } else {
            count++;
        }
        counts.put(c, count);
    }
    // find one of the most frequent character
    char maxChar = 0;
    int maxCount = 0;
    for (Map.Entry<Character,Integer> e: counts.entrySet()) {
        char c = e.getKey();
        int count = e.getValue();
        if (count > maxCount) {
            maxChar = c;
            maxCount = count;
        }
    }
    // replace and return
    return text.replace(maxChar, withChar);
}
0 голосов
/ 14 июня 2019

Спасибо всем. Я решил так:

public class ProvaEsercizio9 {

    public void myMethod(String parola, char c) {
        char myChar = ' ';
        int max = 0;

        for (int i = 0; i < parola.length(); i++) {
            if ( ProvaEsercizio9.countCharOccurrences(parola, parola.charAt(i)) > max) {
                max=ProvaEsercizio9.countCharOccurrences(parola, parola.charAt(i));
                myChar=parola.charAt(i);
            }
        }

        for (int i = 0; i < parola.length(); i++) {
            if(parola.charAt(i)==myChar)
                parola=parola.replace(myChar, c);
        }
        System.out.println(parola);
    }


    public static int countCharOccurrences(String source, char target) {
        int counter = 0;

        for (int i = 0; i < source.length(); i++) {
            if (source.charAt(i) == target) {
                counter++;
            }
        }
        return counter;
    }
}
0 голосов
/ 14 июня 2019

Вот решение, попытайтесь понять его и улучшить его, удачи

public class Main
{
    public static void myMethod(String text, char x) {
        int pos = 0;
        int max = 0;
        int tmp = 0;

        for (int i = 0; i < text.length(); i++) {
            tmp = countCharOccurrences(text, text.charAt(i));
            if (tmp > max) {  
                pos = i;
                max = tmp;
            }
        }
        System.out.println("The char at :{" + pos + "} with the value :{" + text.charAt(pos) + "} is the char with most occur");
    }


    public static int countCharOccurrences(String source, char target) {
        int counter = 0;

        for (int i = 0; i < source.length(); i++) {
            if (source.charAt(i) == target) {       
                counter++;
            }
        }
        return counter;
    }
    public static void main(String[] args) {
        myMethod("ovololloll", 'x');
    }
}
...