Write the function string PluralForm(String word) which takesa standard English
ID: 3732972 • Letter: W
Question
Write the function string PluralForm(String word) which takesa standard English word as a parameter and returns the plural form based on the following English rules. Punctuation should be preserved If the word ends in s, x, z, ch, or sh, add es to the end of the word If the word ends in o preceded by a consonant, add es to the end of the word If the word ends in y preceded by a consonant, replace the y with ies. . For all other cases, add an s to the end of the word. Example runs are shown below This program converts an English word to its plural form English word: toy Plural form: toysExplanation / Answer
import java.util.Scanner;
class Main {
public static String PluralForm(String word)
{
int l = word.length();
// case 1
if(word.charAt(l-1) == 's' || word.charAt(l-1) == 'x' || word.charAt(l-1) == 'z' || (word.charAt(l-1) == 'h' && (word.charAt(l-2) == 's' || word.charAt(l-2) == 'c')))
{
word = word + "es";
}
// case 2
else if(word.charAt(l-1) == 'o' && word.charAt(l-2) != 'a' && word.charAt(l-2) != 'e' && word.charAt(l-2) != 'i' && word.charAt(l-2) != 'o' && word.charAt(l-2) != 'u' )
{
word = word + "es";
}
// case 3
else if(word.charAt(l-1) == 'y' && word.charAt(l-2) != 'a' && word.charAt(l-2) != 'e' && word.charAt(l-2) != 'i' && word.charAt(l-2) != 'o' && word.charAt(l-2) != 'u' )
{
word = word.substring(0, l-1) + "ies";
}
// case 4
else
{
word = word + 's';
}
return word;
}
public static void main(String[] args) {
System.out.println("This program converts an English word to its plural form. ");
Scanner sc = new Scanner(System.in);
System.out.print("English word: ");
String w = sc.next();
System.out.print("Plural form: "+PluralForm(w));
}
}
/* SAMPLE OUTPUT
This program converts an English word to its plural form.
English word: toy
Plural form: toys
This program converts an English word to its plural form.
English word: tax
Plural form: taxes
This program converts an English word to its plural form.
English word: mango
Plural form: mangoes
This program converts an English word to its plural form.
English word: twenty
Plural form: twenties
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.