Returns the reversed vowel of a string. The other words remain unchanged.

Source: Internet
Author: User

Returns the reversed vowel of a string. The other words remain unchanged.

Train of Thought: Use ArrayList to store vowels (because there is a contains method), and record the number of vowels and put them into the stack (reverse similar to stack first and then out), which uses string and char array conversion.

Import java. util. arrayList; import java. util. list; import java. util. imports; import java. util. stack; public class reverseWords {public static String reverseVowels (String s) {Stack <String> replace = new Stack <String> (); // use the stack to output the List <Character> contain = new ArrayList <Character> (); // store the vowel int count = 0; // The number of vowel letters contain. add ('A'); contain. add ('E'); contain. add ('I'); contain. add ('O'); contain. add ('U'); contain. add ('A'); contain. add ('E'); contain. add ('I'); contain. add ('O'); contain. add ('U'); char [] ch = s. toCharArray (); for (int I = 0; I <s. length (); I ++) {if (contain. contains (ch [I]) count ++;} if (count <2) return s; // The number of vowels smaller than two does not need to be reversed int [] num = new int [count]; // record the vowel position int j =-1; for (int I = 0; I <ch. length; I ++) {if (contain. contains (ch [I]) {num [++ j] = I; replace. push (String. valueOf (ch [I]); // stack feed} for (int I = 0; I <count; I ++) {ch [num [I] = replace. pop (). charAt (0);} return String. valueOf (ch); // toString ()} public static void main (string [] args) cannot be used to convert a char array to a String) {// TODO Auto-generated method stub initialize partition = new partition (System. in); while (partial. hasNext () {String str = success. next (); System. out. println (reverseVowels (str ));}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.