package eecs1022.finalb; /** * This question is worth 2.5 marks. No partial marks will be awarded for this question. */ public class Question7 { /** * Returns the number of characters that occur more than once in the given string. * You may assume that the string is not null and not empty. You may may also assume * that all characters are lower case letters. * * @param s a string. * @return the number of characters that occur more than once in the given string. */ public static int moreThanOnce(String s) { int number = 0; for (char c = 'a'; c <= 'z'; c++) { int count = 0; for (int i = 0; i < s.length(); i++) { char letter = s.charAt(i); if (letter == c) { count++; } } if (count > 1) { number++; } } return number; } /** * Tests method. You may add more test cases. * * @param args not applicable. */ public static void main(String[] args) { System.out.println(Question7.moreThanOnce("a")); System.out.println(Question7.moreThanOnce("accb")); System.out.println(Question7.moreThanOnce("abacb")); System.out.println(Question7.moreThanOnce("abca")); System.out.println(Question7.moreThanOnce("aaabbcc")); } }