w3resource

Java programming Exercises: Last n vowels of a given string

Java Regular Expression: Exercise-25 with Solution

Write a Java program to get last n vowels of a given string.

Sample Solution:

Java Code:

public class test {
 
   public static void main(String[] args) {	   
	    String text ="Java";
		int n = 2;
		System.out.println("Original String: "+text);
		System.out.println("Last "+ n + " vowels of a given string: "+validate(text,n));
		text ="JavaScript";
		n = 2;
		System.out.println("Original String: "+text);
		System.out.println("Last "+ n + " vowels of a given string: "+validate(text,n));
		n = 3;
		System.out.println("Last "+ n + " vowels of a given string: "+validate(text,n));
		text ="SQLite";
		n = 2;
		System.out.println("Original String: "+text);
		System.out.println("Last "+ n + " vowels of a given string: "+validate(text,n));
		text ="The quick brown fox jumps over the lazy dog.";
		n = 2;
		System.out.println("Original String: "+text);
		System.out.println("Last "+ n + " vowels of a given string: "+validate(text,n));
		n = 3;
		System.out.println("Original String: "+text);
		System.out.println("Last "+ n + " vowels of a given string: "+validate(text,n));
		n = 15;
		System.out.println("Original String: "+text);
		System.out.println("Last "+ n + " vowels of a given string: "+validate(text,n));		
	}

   public static String validate(String text, int n) {
		String s = text.replaceAll("(?i)[^aeiou]", "");
		int l = s.length();
		return (n > s.length()) ? "Number of vowels mismatch!!!" : s.substring(s.length() - n);
   }
}

Sample Output:

Original String: Java
Last 2 vowels of a given string: aa
Original String: JavaScript
Last 2 vowels of a given string: ai
Last 3 vowels of a given string: aai
Original String: SQLite
Last 2 vowels of a given string: ie
Original String: The quick brown fox jumps over the lazy dog.
Last 2 vowels of a given string: ao
Original String: The quick brown fox jumps over the lazy dog.
Last 3 vowels of a given string: eao
Original String: The quick brown fox jumps over the lazy dog.
Last 15 vowels of a given string: Number of vowels mismatch!!!

Pictorial Presentation:

Java Regular Expression: Last n vowels of a given string.

Flowchart :

Flowchart: Last n vowels of a given string.

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Separate consonants and vowels from a given string.

Next: Check whether a given string is a valid hex code or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Share this Tutorial / Exercise on : Facebook and Twitter

Java: Tips of the Day

What is the best way to filter a Java Collection?

Java 8 (2014) solves this problem using streams and lambdas in one line of code:

List<Person> beerDrinkers = persons.stream()
    .filter(p -> p.getAge() > 16).collect(Collectors.toList());

Use Collection#removeIf to modify the collection in place. (Notice: In this case, the predicate will remove objects who satisfy the predicate):

persons.removeIf(p -> p.getAge() <= 16);

lambdaj allows filtering collections without writing loops or inner classes:

List beerDrinkers = select(persons, having(on(Person.class).getAge(),
    greaterThan(16)));

Ref: https://bit.ly/3uwYid6