In the above Java program to replace all the vowels in a string with a specified character we created a function named validate() and then with the help of a built-in function “replaceAll”, replaced the vowel with passed string.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 | import java.util.Scanner; public class replace{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print( "Input the string: " ); String txt = in.nextLine(); System.out.println( "Original string: " +txt); System.out.println( "After replacing vowel(s) with specified Character: " +validate(txt, '#' )); } public static String validate(String txt, char chr) { return txt.replaceAll( "[aeiouAEIOU]" , "" + chr); } } |
Sample Output: Input the string: lazy frog Original string: lazy frog After replacing vowel(s) with specified Character: l#zy fr#g