programming in java 1)If a word ends in \'a\', append an \'r\'. For example \"tu
ID: 3819371 • Letter: P
Question
programming in java
1)If a word ends in 'a', append an 'r'. For example "tuna" becomes "tunar", "Cuba" becomes "Cubar", and "idea" becomes "idear". (Don't change this to an 'h' based on the previous rule; leave it as an 'r'.) Do not apply this rule to the word "a", so "a tuna" should become "a tunar", not "ar tunar".
2)Replace the word "very" with the word "wicked". So "very hard" becomes "wicked hahd".
Make sure that these rules apply to capitalized words/letters as well as lower-case.
3)If 'r' is at the end of a word and is preceded by "ee" or 'i' replace 'r' with "yah" instead of 'h'. For example, "deer" becomes "deeyah" instead of "deeh", but "veneers" still becomes "veneehs".
4)If 'r' is at the end of a word and is preceded by "oo", replace 'r' with "wah". For example, "door" becomes "doowah" instead of "dooh" (but "doors" still becomes "doohs").
Explanation / Answer
import java.util.Scanner;
import java.util.StringTokenizer;
public class WordsMagic {
public static void main(String[] args) {
//Declaring String objects
String word1,word2,word3,word4;
StringBuffer sb;
//Creating Scanner Object
Scanner s = new Scanner(System.in);
//reading first word
System.out.println("******First Word******");
System.out.println("Enter the word1:");
word1 = s.nextLine();
//Checking the word endswith a
if(word1.endsWith("a")){
sb = new StringBuffer(word1);
if(word1.equals("a")){
System.out.println(sb.toString());
}else{
sb.append("r");
System.out.println(sb.toString());
}
}else{
System.out.println(word1);
}
System.out.println("******Second Word******");
System.out.println("Enter the word2:");
word2 = s.nextLine();
sb = new StringBuffer();
int i=0;
//Loop for checking Capitilize or lower case letters or upper case
StringTokenizer st = new StringTokenizer(word2);
while(st.hasMoreTokens()){
String token = st.nextToken();
if(token.equalsIgnoreCase("very")){
sb.append("very ");
}else{
sb.append(token+" ");
}
}
//Replacing very with wicked
word2 = sb.toString().replaceAll("very", "wicked");
System.out.println(word2);
System.out.println("******Third Word******");
System.out.println("Enter the word3:");
word3 = s.nextLine();
//Checking words ends with r
if(word3.endsWith("r")){
sb = new StringBuffer(word3);
sb.deleteCharAt(word3.length()-1);
//Checking words preceeds with ee or i
if(sb.toString().endsWith("ee") || sb.toString().endsWith("i")){
sb.append("yah");
System.out.println(sb);
}
//Checking words contains r
}else if(word3.contains("r")){
System.out.println(word3.replaceAll("r", "h"));
}else{
System.out.println(word3);
}
System.out.println("******Fourth Word******");
System.out.println("Enter the word4:");
word4 = s.nextLine();
//Checking word ends with r
if(word4.endsWith("r")){
sb = new StringBuffer(word4);
sb.deleteCharAt(word3.length()-1);
//Checking word preceeds with oo
if(sb.toString().endsWith("oo")){
sb.append("wah");
System.out.println(sb);
}
//Checking word contains r
}else if(word4.contains("r")){
System.out.println(word4.replaceAll("r", "h"));
}else{
System.out.println(word4);
}
}
}
Output1:
******First Word******
Enter the word1:
a cuba
a cubar
******Second Word******
Enter the word2:
ver hard and very bad
ver hard and wicked bad
******Third Word******
Enter the word3:
deer
deeyah
******Fourth Word******
Enter the word4:
door
doowah
Output2:
******First Word******
Enter the word1:
a luna
a lunar
******Second Word******
Enter the word2:
Very hard and VEry bad
wicked hard and wicked bad
******Third Word******
Enter the word3:
veneers
veneehs
******Fourth Word******
Enter the word4:
doors
doohs
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.