Tannia
Tannia

Reputation: 89

How to avoid counting the repeated vowel?

I have written the code for the first part, but it counts the number of vowels with repetitions included, but I also want to know how to count the number of vowels without repetitions.

Also I am struggling to write the second part of the code, that is, report the sum of vowels.

Here's what I have written so far:

import java.io.*;
public class CountVowel {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the string:");
        String s = br.readLine();
        int l = s.length();
        char ch;
        int i;
        int count = 0;
        for(i = 0; i < l; i++)
        {
            ch = s.charAt(i);
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
            {
                count=count+1;
            }
        }
        System.out.println("The number of vowels are:"+count);
    }
}

Upvotes: 3

Views: 1898

Answers (4)

sam
sam

Reputation: 2033

Here is a alternate approach using two arraylists. One contains vowels and other one is empty say charsInString. We can add into the other one if we encounter an vowel which is not present in the charsInString. We can find whether an vowel has been added by using indexOf

List<Character> chars = new ArrayList<Character>(Arrays.asList('a','e','i','o','u'));
List<Character> charsInString = new ArrayList<Character>();
String test = "this is a test string";

for (char a: test.toCharArray()) {
    if (chars.indexOf(a) > -1) {
        if (charsInString.indexOf(a) == -1) {
            charsInString.add(a);
        }
    }
}

System.out.println(charsInString.size());   //answer is 3

Demo

Upvotes: 0

burglarhobbit
burglarhobbit

Reputation: 2291

I suggest to do it like this. This will output the number of vowels as you want without repetition:

int count = 0;
boolean[] vowel = {false, false, false, false, false};
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
for(int i=0;i<l;i++) { //This will traverse every letter for the string
    ch=s.charAt(i);
    for(int j=0; j<5; j++) { //This will search through vowels
        //Ff any vowels are matched the count will increase,
        //But if they are already matched, count will not increase
        if(ch==vowels[j] && !vowel[j]) {
            count++;
            vowel[j] = true;
        }
    }
}
System.out.println("The number of vowels are:" + count);

Upvotes: 0

ccc
ccc

Reputation: 370

Simply, you try with Set interface that not stores any duplicates, use following code,

    public static void main(String[] args) throws IOException {

    Set<Character> set = new HashSet<>();
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the string:");
    String s = br.readLine();
    int l = s.length();
    char ch;
    int i;
    int count = 0;
    for (i = 0; i < l; i++) {
        ch = s.charAt(i);
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
            set.add(ch);
        }
    }

    System.out.println("The number of vowels are:" + set.size());

}

Upvotes: 3

Tim B
Tim B

Reputation: 41208

There is a neater way to do this:

Set<Character> vowels = new HashSet<Character>();
vowels.add('a');
vowels.add('e');
vowels.add('i');
vowels.add('o');
vowels.add('u');

for(i=0;i<l;i++) {
    ch=s.charAt(i);
    vowels.remove(ch);
}

System.out.println("The number of vowels are:" + 5-vowels.size());

Upvotes: 1

Related Questions