Ramsey
Ramsey

Reputation: 133

Print the number of unique vowels in a string, Java

I need to find the number of distinct vowels. I came up with the code below but it can't make distinction between same vowels:

public static int count_Vowels(String str) {
    str = str.toLowerCase();
    int count = 0;
    for (int i = 0; i < str.length(); i++) {
        if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i'
                || str.charAt(i) == 'o' || str.charAt(i) == 'u') {
            count++;
        }
    }
    return count;
}

Upvotes: 1

Views: 5098

Answers (11)

ilandirayan
ilandirayan

Reputation: 1

  @Test
public void numfindVoweles(){
    String s="aeiouaedtesssiou";
    char a[]=s.toCharArray();
    HashMap<Character,Integer> hp= new HashMap<Character, Integer>();
    for(char ch:a){
        if(hp.containsKey(ch) && (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')){
           hp.put(ch,hp.get(ch)+1);
       }
        else if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u'){
            hp.put(ch,1);
        }
    }
    System.out.println(hp);

}

Upvotes: 0

Benjamin Ross
Benjamin Ross

Reputation: 1

Here is a solve for this problem without using objects. It's a crude but great solve for beginners who encounter this problem with limited js experience.

How to count unique vowels is a string;

function hasUniqueFourVowels(str){
let va = 0
let ve = 0  
let vi = 0
let vo = 0
let vu = 0
let sum = 0
for(let i = 0; i < str.length; i++){
 let char = str[i];
   if(char === "i"){
    vi = 1
  }
    if(char === "e"){
    ve = 1
  }
   if(char === "a"){
    va = 1
  }
  if(char === "o"){
   vo = 1
   }
  if(char === "u"){
  vu = 1
  }
  sum = va + vi + vo + ve + vu
  if (sum >= 4){
   return true
   }
 }
   return false

 }

Upvotes: 0

Dhruv Sharma
Dhruv Sharma

Reputation: 1

You can use this Method to Find Count of Distinct vowels.

public static int count_Vowels(String str) {
    char[] c = str.toLowerCase().toCharArray();
    int Counter=0;
     String NewString="";
     for(int i=0;i<c.length;i++){
         String tempString="";
         tempString+=c[i];
         if(!NewString.contains(tempString) && (c[i]=='a'||c[i]=='e'||c[i]=='i'||c[i]=='o'||c[i]=='u')){
             Counter++;
             NewString+=c[i];
         }
     }
    return Counter;
}

Upvotes: 0

anjali
anjali

Reputation: 1

public static void main(String args[]) {
    String sentence;
    int v=0,c=0,ws=0;
    Scanner sc= new Scanner(System.in);
    sentence = sc.nextLine();
    sc.close();
    sentence.toLowerCase();
    String res="";
    for(int i=0;i<sentence.length();i++) {

        if(sentence.charAt(i)=='a'||sentence.charAt(i)=='e'||sentence.charAt(i)=='i'||sentence.charAt(i)=='o'||sentence.charAt(i)=='u') {
        if(res.indexOf(sentence.charAt(i))<0) {
            res+=sentence.charAt(i);
        v++;
        }//System.out.println(res.indexOf(sentence.charAt(i)));
            }
        else if(sentence.charAt(i)==' ')
            ws++;
        else c++;
    }
    System.out.println(res);
    System.out.println("no of vowels: "+v+"\n"+"no of consonants: "+c+"\n"+"no of 
   white spaces: "+ws);
}

Upvotes: 0

Shoeb Shaul
Shoeb Shaul

Reputation: 1

static void vow(String input){          
    String output=input.toLowerCase();
    int flaga=0,flage=0,flagi=0,flago=0,flagu=0;
    for(int i=0;i<input.length();i++) {
        if((output.charAt(i))=='a' && flaga==0) {
            System.out.print(input.charAt(i)+" ");
            flaga++;
        }
        if(output.charAt(i)=='e' && flage==0) {
            System.out.print(input.charAt(i)+" ");
            flage++;
        }
        if(output.charAt(i)=='i' && flagi==0) {
            System.out.print(input.charAt(i)+" ");
            flagi++;
        }
        if(output.charAt(i)=='o' && flago==0) {
            System.out.print(input.charAt(i)+" ");
            flago++;
        }
        if(output.charAt(i)=='u' && flagu==0) {
            System.out.print(input.charAt(i)+" ");
            flagu++;
        }
    }
}

Upvotes: 0

Juan Carlos Mendoza
Juan Carlos Mendoza

Reputation: 5814

The problem in your code is that you are not counting the distinct vowels, but all the vowels in the string. A Java-8 way to this:

public static int countDistinctVowels(String str) {
    str = str.toLowerCase();
    int count = (int) str.chars()                     // get IntStream of chars
            .mapToObj(c -> (char) c)                  // cast to char
            .filter(c -> "aeiou".indexOf(c) > -1)     // remove all non-vowels
            .distinct()                               // keep the distinct values
            .count();                                 // count the values

    return count;
}

Also use proper Java naming conventions: countDistinctVowels, no count_Distinct_Vowels.

Upvotes: 1

Przemysław Moskal
Przemysław Moskal

Reputation: 3609

I made explanations in the comments to the code:

public static int count_Vowels(String str) {
        str = str.toLowerCase();
        Set<Character> setOfUsedChars = new HashSet<>(); // Here you store used vowels
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i'
                    || str.charAt(i) == 'o' || str.charAt(i) == 'u') { // if currently checked character is vowel...
                setOfUsedChars.add(str.charAt(i)); // add this vowel to setOfUsedChars
            }
        }
        return setOfUsedChars.size(); // size of this sets is a number of vowels present in input String
}

Upvotes: 0

morizmartiner
morizmartiner

Reputation: 311

You could use the method contains

public static int count_Vowels(String str) {
    str = str.toLowerCase();
    int count = 0;
    count += string.contains("a") ? 1 : 0;
    count += string.contains("e") ? 1 : 0;
    count += string.contains("i") ? 1 : 0;
    count += string.contains("o") ? 1 : 0;
    count += string.contains("u") ? 1 : 0;
    return count;
}

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201467

I would start with five variables (one for each vowel) set to 0, iterate the characters in the input and set the corresponding variable to 1 if I find a match, and simply return the accumulated value of said variables. Like,

public static int count_Vowels(String str) {
    int a = 0, e = 0, i = 0, o = 0, u = 0;
    for (char ch : str.toLowerCase().toCharArray()) {
        if (ch == 'a') {
            a = 1;
        } else if (ch == 'e') {
            e = 1;
        } else if (ch == 'i') {
            i = 1;
        } else if (ch == 'o') {
            o = 1;
        } else if (ch == 'u') {
            u = 1;
        }
    }
    return a + e + i + o + u;
}

Upvotes: 6

user3335083
user3335083

Reputation:

there's definitely an issue here with this counting. at the very least. you should rethink this:

if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i'
                || str.charAt(i) == 'o' || str.charAt(i) == 'u')

            count++;

Upvotes: 0

amalgamat
amalgamat

Reputation: 130

You could use Set data structure and instead of incrementing the counter just add vowels to the set. At the end you can return just the size of the set.

Upvotes: 2

Related Questions