Counting unique characters in a String given by the user

前端 未结 12 1404
情书的邮戳
情书的邮戳 2021-02-15 17:42

I have to write a program that counts the uniques characters in a String given by the user. For example \"abc\" returns 3 and \"aabbccd\" returns 4. I am not allow to use advan

12条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-15 18:18

    Try to see if the following code helps you:

    String myString = "";
    
    for(int i=0; i< word.length(); i++) {
    
        if(myString.indexOf(word.charAt(i)) == -1) {
            System.out.println(word.charAt(i));
            myString = myString + word.charAt(i);
        }
    }
    return myString.length();
    

提交回复
热议问题