-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordPlay.java
48 lines (46 loc) · 1.51 KB
/
WordPlay.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class WordPlay {
/* isVowel */
public static boolean isVowel(char ch){
String vowels = "aeiouAEIOU";
if(vowels.indexOf(ch) != -1){
return true;
}
return false;
}
/* replaceVowels */
public static String replaceVowels(String phrase, char ch){
StringBuilder sb = new StringBuilder(phrase);
for(int i=0; i<phrase.length(); i++){
char currChar = phrase.charAt(i);
if(isVowel(currChar)){
sb.setCharAt(i, ch);
}
}
return sb.toString();
}
/* emphasize */
public static String emphasize(String phrase, char ch){
StringBuilder sb = new StringBuilder(phrase);
for(int i=0; i<phrase.length(); i++){
char currChar = phrase.charAt(i);
if(Character.toLowerCase(currChar) == Character.toLowerCase(ch)){
if(i%2 == 0){
sb.setCharAt(i, '*');
}
else{
sb.setCharAt(i, '+');
}
}
}
return sb.toString();
}
/* main function to test the WordPlay class */
public static void main(String[] args){
char ch = 'F';
System.out.println(isVowel(ch));
String phrase = "Hello World";
System.out.println(replaceVowels(phrase, '*'));
String phrase2 = "Mary Bella Abracadabra";
System.out.println(emphasize(phrase2, 'a'));
}
}