w3resource

Java: Last n vowels of a given string


25. Get Last N Vowels

Write a Java program to get the 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.


For more Practice: Solve these Related Problems:

  • Write a Java program to extract the last n vowels from a string using regex and substring methods.
  • Write a Java program to reverse a string and then select the first n vowels from the reversed string, restoring their order.
  • Write a Java program to implement a method that scans a string from the end and collects n vowels encountered.
  • Write a Java program to return the last n vowels in a string while ignoring case differences.

Go to:


PREV : Separate Consonants and Vowels.

NEXT : Validate Hex Code.

Java Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.