Java examples for Language Basics:String
Uses substrings to replace all the vowels in a string entered by the user with asterisks:
import java.util.Scanner; public class MarkVowels { static Scanner sc = new Scanner(System.in); public static void main(String[] args) {//from w w w . ja v a 2 s .c om System.out.print("Enter a string: "); String s = sc.nextLine(); String originalString = s; int vowelCount = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if ( (c == 'A') || (c == 'a') || (c == 'E') || (c == 'e') || (c == 'I') || (c == 'i') || (c == 'O') || (c == 'o') || (c == 'U') || (c == 'u') ){ String front = s.substring(0, i); String back = s.substring(i+1); s = front + "*" + back; } } System.out.println(); System.out.println(originalString); System.out.println(s); } }