Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- userText = userText.toLowerCase(); // userText is declared earlier in the program
- // as the user's input. Setting this to lowercase
- // so it doesn't say "a" and "A" are two different
- // characters.
- int uniqueChars = 0;
- for (int i = 0; i < lengthText-1; i++) { // lengthText is declared earlier
- // as userText.length();
- if (userText.charAt(i) != userText.charAt(i+1))
- uniqueChars++;
- }
- System.out.println("there are " + (uniqueChars + 1) + " unique characters in your string.");
- }
- public static int countUniqueCharacters(String s) {
- String lowerCase = s.toLowerCase();
- char characters[] = lowerCase.toCharArray();
- int countOfUniqueChars = s.length();
- for (int i = 0; i < characters.length; i++) {
- if (i != lowerCase.indexOf(characters[i])) {
- countOfUniqueChars--;
- }
- }
- return countOfUniqueChars;
- }
- char[] letters = new char[26];
- for (char c : letters)
- {
- letters[c]=0;
- }
- v = sort(v);//your sort method
- int count = 0;
- for (int i = 0;i< lengthText-1; i++)
- { if v[i] == v[i + 1] {
- i++;
- } else {
- count++;
- }
- }
- list = new ArrayList<String>();
- for ( /* */ ) { // same for loop you wrote
- String character = (String) text.charAt(i);
- if(!list.contains(character)) { // note the '!'
- list.add(character);
- }
- }
- // and finally
- int quantity = list.size();
- public static int countUniqueCharacters(String input)
- {
- String unique = input.replaceAll("(.)(?=.*?\1)", "");
- return unique.length();
- }
- public static int countUniqueCharacters(String input)
- {
- String unique = input.replaceAll("(?i)(.)(?=.*?\1)", "");
- return unique.length();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement