Table of contents
1.
Introduction
2.
Approach
3.
Using if-else statement
3.1.
Method
4.
Using Switch-case statement
4.1.
Method
5.
Using if-else statement in a Method
5.1.
Method
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024

Vowels in a String

Author Nagendra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Java is one of the most popular programming languages. Learning Java reinforces essential computer science principles while also providing access to a wide range of professional options. 

A string is a collection of characters in Java, while a char is a single number used to hold variables. In Java, the char utilises two bytes. The BufferedReader and InputStreamReader classes in Java are used to read the user's input from the keyboard. The readLine() function is then used to read a line.
Some facts about Strings are : 

 

Approach

We use the Brute-Force approach to check if every character present in the String is a vowel or not. This can be checked by the following two methods :

  1. Using if-else statement
  2. Using Switch-case statement
  3. Using if-else statement in a Method

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

  1. Are Strings in Java mutable?
    No, Strings in Java are immutable.
     
  2. 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.
     
  3. 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!

 

Live masterclass