Counting Vowels in a String - Time Complexity - Java 2024

 Counting Vowels in a String

        We have two String's, one has the value we need to check and another has a vowels. 

1. First we need a for loop for iteration of the given String.

2. Second for the condition end value is String's length, so to achieve Time Complexity we are storing the size of the String in one variable.

3. Next step as inside the for loop take the each character from the string and check if that character is in the vowels string.

4. If the condition is true increase the count values as one using post increment operator. If not doesn't do anything move to next iteration.

Code :

package com.dinatechy.challange;


public class CountVowels {

public static void main(String[] args) {

//Count number of Vowels - a e i o u

String value = "DinaTechy";// vowels = 3

String vowels = "aeiou";

int count = 0;

int size = value.length();

for(int i=0; i<size; i++) {

if(vowels.contains(String.valueOf(value.charAt(i))))

count++;

}

System.out.println("Vowels Count : "+count);

}


}

Output : 

Vowels Count : 3


Follow us for more Interesting Java Programming Question with answers....

Comments

Popular posts from this blog

Finding Second largest number in Array - Java - TCS - Technical Interview

Checking Character only in String - Java 2024 - MNC - Technical Interview

Object Class in Java | Article | Solution Maker - Blog