Javatpoint Logo
Javatpoint Logo

Write a Program to Print Reverse of a Vowels String in Java

In this section, we will be discussing how to print the reverse of a vowel string in Java. Vowels are the letters "a", "e", "i", "o", and "u", and a vowel string is a string that contains only vowels. We will first define the problem statement and then discuss different approaches to solve the problem.

Problem Statement

Given a vowel string, we need to print its reverse. For example, if the input string is "aeiou", the output should be "uoiea".

Approaches

There are multiple ways to solve the problem. In this article, we will discuss two different approaches.

1. Using StringBuilder and loop

The first approach is to use StringBuilder and loop to reverse the vowel string. We can create a StringBuilder object and append the vowels from the input string in reverse order. The following code demonstrates this approach.

One important thing to note here is that we are iterating over the input string in reverse order. This is because we want to append the vowels to the StringBuilder object in reverse order.Another important point to consider is that we are using the isVowel method to check whether a character is a vowel or not. This method returns true if the character is one of the vowels 'a', 'e', 'i', 'o', or 'u' (in both lowercase and uppercase).

The StringBuilder class is a mutable sequence of characters. It is more efficient than using a String object when we need to make frequent modifications to the string. In this case, we are appending the vowels to the StringBuilder object one by one in reverse order. Finally, we convert the StringBuilder object to a String object using the toString() method and return it.

Reverse.java

Output:

o o e

The reverseVowels method takes an input string s and returns the reverse of the vowels in the string. The isVowel method checks whether a given character is a vowel or not. The main loop iterates over the characters of the input string in reverse order and appends the vowels to the StringBuilder object. Finally, the StringBuilder object is converted to a string and returned.

2. Using two pointers

The second approach is to use two pointers to swap the vowels in the input string. We can initialize two pointers, one at the start of the string and the other at the end of the string. We then move both pointers towards each other until they meet. If both pointers are pointing to vowels, we swap them. Thsse following code demonstrates this approach.

Reverse.java

Output:

o o e

The reverseVowels method takes an input string s and returns the reverse of the vowels in the string. The isVowel method checks whether a given character is a vowel or not. The main loop iterates over the characters of the input string using two pointers i and j. If both pointers are pointing to vowels, we swap them and move both pointers towards each other. If one of the pointers is not pointing to a vowel, we move that pointer towards the other pointer.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA