Java Examples

Java program to replace vowels with star

program

package com.candidjava;
 
import java.util.*;
 
public class StringVovelReplaceStar
{
	static Scanner sc = new Scanner(System.in);
 
	public static void main(String[] args)
	{
		System.out.print("Enter a string: ");
		String s = sc.nextLine();
		String originalString = s;
 
		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(originalString);
		System.out.println(s);
	}
 
}

Output

Enter a string: Ram bought apple and umbrella in a big shop

Ram bought apple and umbrella in a big shop

R$m b$$ght $ppl$ $nd $mbr$ll$ $n $ b$g sh$p