How to count total no. of vowels in a String | Java Programming | AskTheCode
Problem:
Java program to count how many vowels have been used in a user given String.
There can be several ways to count the number of vowels in a String. Here, we've published
5 different ways to count the number of vowels in a String in Java. The three approaches are normal String iterative methods, that count the vowel during iteration. The rest two approaches are recursion-based.
Input Sample:
Hello Coders, Welcome to AskTheCode.
Output Sample:
12
Code:
Method 1:
import java.util.Scanner;
class VowelsInString{
public static void main(String[] args) {
try{
System.out.print("Enter a string: ");
String str = read();
write("The string is "+str+"\n");
write("Total vowels found: "+countVowels(str)+"\n");
} catch (Exception e){
System.out.print(e+" raised!\n");
};
}
private static int countVowels(String s){
int count = 0;
for (char c : s.toCharArray()){
if ((c == 65) || (c == 97) || (c == 69) || (c == 101) || (c == 73) || (c == 105) || (c == 79) || (c == 111) || (c == 85) || (c == 117))
count++;
}
return count;
}
protected static String read() throws java.lang.Exception{
Scanner sc=new Scanner(System.in);
return sc.nextLine();
}
protected static void write(String s){
System.out.print(s);
}
}
Method 2:
import java.util.Scanner;
class VowelsInString{
public static void main(String[] args) {
try{
System.out.print("Enter a string: ");
String str = read();
write("The string is "+str+"\n");
write("Total vowels found: "+countVowels(str)+"\n");
} catch (Exception e){
System.out.print(e+" raised!\n");
};
}
private static int countVowels(String s){
int count = 0;
for (char c : s.toCharArray()){
if ((c == 'A') || (c == 'a') || (c == 'E') || (c == 'e') || (c == 'I') || (c == 'i') || (c == 'O') || (c == 'o') || (c == 'U') || (c == 'u'))
count++;
}
return count;
}
protected static String read() throws java.lang.Exception{
Scanner sc=new Scanner(System.in);
return sc.nextLine();
}
protected static void write(String s){
System.out.print(s);
}
Method 3:
import java.util.Scanner;
class VowelsInString{
public static void main(String[] args) {
try{
System.out.print("Enter a string: ");
String str = read();
write("The string is "+str+"\n");
write("Total vowels found: "+countVowels(str)+"\n");
} catch (Exception e){
System.out.print(e+" raised!\n");
};
}
private static int countVowels(String s){
int count = 0;
for (int i=0; i<s.length(); i++){
if ((s.charAt(i) == 'A') || (s.charAt(i) == 'a') || (s.charAt(i) == 'E') || (s.charAt(i) == 'e') || (s.charAt(i) == 'I') || (s.charAt(i) == 'i') || (s.charAt(i) == 'O') || (s.charAt(i) == 'o') || (s.charAt(i) == 'U') || (s.charAt(i) == 'u'))
count++;
}
return count;
}
protected static String read() throws java.lang.Exception{
Scanner sc=new Scanner(System.in);
return sc.nextLine();
}
protected static void write(String s){
System.out.print(s);
}
}
Method 4:
import java.util.Scanner;
class VowelsInString{
public static void main(String[] args) {
try{
System.out.print("Enter a string: ");
String str = read();
write("The string is "+str+"\n");
write("Total vowels found: "+countVowels(str, 0)+"\n");
} catch (Exception e){
System.out.print(e+" raised!\n");
};
}
private static int countVowels(String s, int index){
if (index >= s.length())
return 0;
int count = (s.charAt(index) == 'A') || (s.charAt(index) == 'a') || (s.charAt(index) == 'E') || (s.charAt(index) == 'e') || (s.charAt(index) == 'I') || (s.charAt(index) == 'i') || (s.charAt(index) == 'O') || (s.charAt(index) == 'o') || (s.charAt(index) == 'U') || (s.charAt(index) == 'u') ? 1 : 0;
return count + countVowels(s, index+1);
}
protected static String read() throws java.lang.Exception{
Scanner sc=new Scanner(System.in);
return sc.nextLine();
}
protected static void write(String s){
System.out.print(s);
}
}
Method 5:
import java.util.Scanner;
class VowelsInString{
public static void main(String[] args) {
try{
System.out.print("Enter a string: ");
String str = read();
write("The string is "+str+"\n");
write("Total vowels found: "+countVowels(str.toCharArray(), 0)+"\n");
} catch (Exception e){
System.out.print(e+" raised!\n");
};
}
private static int countVowels(char[] charArr, int i){
if (i >= charArr.length)
return 0;
char c = charArr[i];
int count = ((c == 65) || (c == 97) || (c == 69) || (c == 101) || (c == 73) || (c == 105) || (c == 79) || (c == 111) || (c == 85) || (c == 117)) ? 1 : 0;
return count + countVowels(charArr, ++i);
}
protected static String read() throws java.lang.Exception{
Scanner sc=new Scanner(System.in);
return sc.nextLine();
}
protected static void write(String s){
System.out.print(s);
}
}
Note: There can be several other ways apart from the ways showed above.
If you can try much more ways than this, then, that's appreciable.
תגובות