Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. userText = userText.toLowerCase(); // userText is declared earlier in the program
  2. // as the user's input. Setting this to lowercase
  3. // so it doesn't say "a" and "A" are two different
  4. // characters.
  5. int uniqueChars = 0;
  6. for (int i = 0; i < lengthText-1; i++) { // lengthText is declared earlier
  7. // as userText.length();
  8. if (userText.charAt(i) != userText.charAt(i+1))
  9. uniqueChars++;
  10. }
  11. System.out.println("there are " + (uniqueChars + 1) + " unique characters in your string.");
  12. }
  13.  
  14. public static int countUniqueCharacters(String s) {
  15. String lowerCase = s.toLowerCase();
  16. char characters[] = lowerCase.toCharArray();
  17. int countOfUniqueChars = s.length();
  18. for (int i = 0; i < characters.length; i++) {
  19. if (i != lowerCase.indexOf(characters[i])) {
  20. countOfUniqueChars--;
  21. }
  22. }
  23. return countOfUniqueChars;
  24. }
  25.  
  26. char[] letters = new char[26];
  27. for (char c : letters)
  28. {
  29. letters[c]=0;
  30. }
  31.  
  32. v = sort(v);//your sort method
  33.  
  34. int count = 0;
  35. for (int i = 0;i< lengthText-1; i++)
  36. { if v[i] == v[i + 1] {
  37. i++;
  38. } else {
  39. count++;
  40. }
  41. }
  42.  
  43. list = new ArrayList<String>();
  44. for ( /* */ ) { // same for loop you wrote
  45. String character = (String) text.charAt(i);
  46.  
  47. if(!list.contains(character)) { // note the '!'
  48. list.add(character);
  49. }
  50. }
  51.  
  52. // and finally
  53. int quantity = list.size();
  54.  
  55. public static int countUniqueCharacters(String input)
  56. {
  57. String unique = input.replaceAll("(.)(?=.*?\1)", "");
  58. return unique.length();
  59. }
  60.  
  61. public static int countUniqueCharacters(String input)
  62. {
  63. String unique = input.replaceAll("(?i)(.)(?=.*?\1)", "");
  64. return unique.length();
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement