Java program to intake string & print occurrence of each character(Java Interview Question

Поделиться
HTML-код
  • Опубликовано: 19 ноя 2024

Комментарии • 2

  • @QAFox
    @QAFox  2 месяца назад

    For any doubts, live training updates, internship program and free Courses, please join our Telegram channel t.me/qafoxoriginal

  • @mustaqimahmad1504
    @mustaqimahmad1504 2 месяца назад

    By using Stream APIs
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter string: ");
    String userInput = scanner.nextLine().trim().replace(" ", "");
    Map characterAndCount = userInput.chars()
    .mapToObj(i -> (char) i)
    .collect(Collectors.groupingBy(Function.identity(),
    Collectors.counting()));
    //to print the characters and its count.
    characterAndCount.forEach((character, aLong) -> System.out.println(character + " : " + aLong));