Return the string length, vowels count, and check if it starts or ends with vowel

import java.util.Arrays;
import java.util.Locale;
import java.util.Scanner;

public class StringFunc2 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.print("Enter your name : ");
String name = sc.nextLine();
name = name.toLowerCase();
int nameLen = name.length();
int vowelCount = 0;
String vowelsList = "aeiou";

for (int i = 0; i < nameLen; i++) {
if(vowelsList.contains(name.charAt(i)+"")){
vowelCount++;
}
}

boolean startsWithVowel = false;
if(vowelsList.contains(name.charAt(0)+"")){
startsWithVowel = true;
}
boolean endsWithVowel = false;
if(vowelsList.contains(name.charAt(nameLen-1)+"")){
endsWithVowel = true;
}

// Printing the output
System.out.println("Entered name : "+name);
System.out.println("Name Length : "+nameLen);
System.out.println("Vowels Length : "+vowelCount);
if(startsWithVowel){
System.out.println("Name starts with vowel");
}else{
System.out.println("Name doesn't starts with vowel");

}
if(endsWithVowel){
System.out.println("Name ends with vowel");
}else{
System.out.println("Name doesn't ends with vowel");

}





}

}


Output:

Enter your name : Aarif

Entered name : aarif

Name Length : 5

Vowels Length : 3

Name starts with vowel

Name doesn't ends with vowel


Comments

Popular posts from this blog

Hackerrank - Quicksort 2 - Sorting

Hackerrank - Day of the Programmer

Hackerrank - UNIQUE ARMSTRONG NUMBER