Using if-else statement
Method
- Convert the String to Upper Case
- Check every character of the String whether it is a vowel or not, using if statement
- Print the result accordingly
The following program explains the use of if-else statement in order to find vowels in a String :
public class Ninja {
public static void main(String ar[])
{
String s="Coding Ninjas";
String str=s.toUpperCase(); //Changing all the Characters to Upper Case
int vowels=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')
{
++vowels; //if a vowel is found, it gets incremented
}
}
if(vowels==0)
System.out.println("There are No Vowels in "+ str );
else
System.out.println("There are "+vowels+" Vowels in "+ s );
}
}

You can also try this code with Online Java Compiler
Run Code
File name : Ninja.java
Output :
There are 4 Vowels in Coding Ninjas

You can also try this code with Online Java Compiler
Run Code
Using Switch-case statement
Method
- Convert the String to UpperCase
- Check every character of the String whether it is a vowel or not, using Switch Case statement
- Print the result accordingly
The following program explains the use of Switch-case statement in order to find vowels in a String :
public class Ninja {
public static void main(String ar[])
{
String s="Coding Ninjas";
String str=s.toUpperCase(); //Changing all the Characters to Upper Case
int vowels=0;
for(int i=0;i<str.length();i++)
{
switch(str.charAt(i))
{
case 'A' : vowels++; break;
case 'E' : vowels++; break;
case 'O' : vowels++; break;
case 'I' : vowels++; break;
case 'U' : vowels++; break;
}
}
if(vowels==0)
System.out.println("There are No Vowels in "+ str );
else
System.out.println("There are "+vowels+" Vowels in "+ s );
}
}

You can also try this code with Online Java Compiler
Run Code
File name : Ninja.java
Output :
There are 4 Vowels in Coding Ninjas

You can also try this code with Online Java Compiler
Run Code
Using if-else statement in a Method
Method
- Define a Method with the original String as parameter and return value.
- Create a new String inside it.
- Convert the String to UpperCase
- Check every character of the String, whether it is a vowel or not, using if-else statement.
- Return the new String from the Method.
- Call the function in the main() Method.
The following program explains the use of if-else statement in a user-defined method in order to find vowels in a String :
public class Ninja {
public static int find_vowels(String s)
{
String str=s.toUpperCase(); //Changing all the Characters to Upper Case
int vowels=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')
{
++vowels; //if a vowel is found, it gets incremented
}
}
return vowels;
}
public static void main(String ar[])
{
String str="Coding Ninjas";
int vowels=find_vowels(str); //Calling the function
if(vowels==0)
System.out.println("There are No Vowels in "+ str );
else
System.out.println("There are "+vowels+" Vowels in "+ str );
}
}

You can also try this code with Online Java Compiler
Run Code
File name : Ninja.java
Output :
There are 4 Vowels in Coding Ninjas

You can also try this code with Online Java Compiler
Run Code
Try it by yourself on Java Online Compiler.
Also check out Addition of Two Numbers in Java here.
FAQs
-
Are Strings in Java mutable?
No, Strings in Java are immutable.
-
What is an if-else statement ?
In an if-else statement, if a particular condition is true, it executes a block of code. Another block of code can be performed if the condition is false.
-
What is the Average Time Complexity of finding vowels in a string?
The Time Complexity of finding vowels in a string is O(n).
Key Takeaways
In this article, we have extensively discussed finding vowels in a string and their implementation in Java. The article explains the different approaches used to find vowels in a string in Java. The use of if-else statements and switch-case statements are explained. There are many more approaches, but these are the most efficient in terms of time and space.
We hope that this blog has helped you enhance your knowledge regarding the reverse of String and if you would like to learn more, check out our articles on Java. Do upvote our blog to help other ninjas grow.
Happy Coding!