Java - Write code to Convert a string to its plural form.

Requirements

Write code to Convert a string to its plural form.

Demo

//package com.book2s;

public class Main {
    /**//from ww  w. jav a2  s.co m
     * Convert a string to its plural form.
     *
     * @param string    the string to pluralize.
     * @return          the pluralized string.
     */
    public static String pluralize(String string) {
        char lastCharacter = string.charAt(string.length() - 1);
        if (lastCharacter == 's')
            return string + "es";
        if (lastCharacter == 'y') {
            String start = string.substring(0, string.length() - 1);
            return start + "ies";
        }
        return string + "s";
    }
}

Related Exercise