Take a String and replace all vowels into Capital case in java


In this core java programming tutorial we will write a program to Take a String and replace all vowels into Capital case in java



Take a String and replace all vowels into Capital case in java -


Example -
original string = java made so easy
After replacing all vowels into capital case = jAvA mAdE sO EAsy

I have written program in 2 ways.


Program/ example 1 - Take a String and replace all vowels into Capital case in java using replaceAll method of String class.
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ReplaceAllVowelsIntoCapitalCaseExample {
   public static void main(String[] args) {
          String str = "java made so easy";
          System.out.println("original string = " + str);
          System.out.println("After replacing all vowels into capital case = " +
                                     str.replaceAll("a", "A").replaceAll("e", "E")
                                     .replaceAll("i", "I").replaceAll("o", "O")
                                     .replaceAll("u", "U"));
   }
}
/* OUTPUT
original string = java made so easy
After replacing all vowels into capital case = jAvA mAdE sO EAsy
*/


Program/ example 2 - Take a String and replace all vowels into Capital case in java using switch case.
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ReplaceAllVowelsIntoCapitalCaseExample1 {
   public static void main(String[] args) {
          String str = "java made so easy";
          System.out.println("original string = " + str);
          char c1[] = str.toCharArray();
          for (int i = 0; i < c1.length; i++) {
                 switch (c1[i]) {
                 case 'a':
                       c1[i] = (char) (c1[i] - 32);
                       break;
                 case 'e':
                       c1[i] = (char) (c1[i] - 32);
                       break;
                 case 'i':
                       c1[i] = (char) (c1[i] - 32);
                       break;
                 case 'o':
                       c1[i] = (char) (c1[i] - 32);
                       break;
                 case 'u':
                       c1[i] = (char) (c1[i] - 32);
                       break;
                 }
          }
          str = new String(c1);
          System.out.println("After replacing all vowels into capital case = "
                       + str);
   }
}
/* OUTPUT
original string = java made so easy
After replacing all vowels into capital case = jAvA mAdE sO EAsy
*/


So in this core java programming tutorial we wrote a program how to Take a String and replace all vowels into Capital case in java


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>


>Pattern/Pyramid generating program in java



No comments:

Post a Comment