w3resource

C#: Check whether a given word is plural or not

C# Sharp Basic: Exercise-77 with Solution

Check Word Plural

Singular means only one and plural means more than one. In order to make a noun plural, it is usually only necessary to add s.

Write a C# Sharp program to check whether a word is plural or not.

Sample Solution:

C# Sharp Code:

using System;

namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            // Displaying whether 'Exercise' is considered plural by calling the test method
            Console.WriteLine("Is 'Exercise' plural? " + test("Exercise"));

            // Displaying whether 'Exercises' is considered plural by calling the test method
            Console.WriteLine("Is 'Exercises' plural? " + test("Exercises"));

            // Displaying whether 'Books' is considered plural by calling the test method
            Console.WriteLine("Is 'Books' plural? " + test("Books"));

            // Displaying whether 'Book' is considered plural by calling the test method
            Console.WriteLine("Is 'Book' plural? " + test("Book"));
        }

        // Method to test if a word is plural based on its ending
        public static bool test(string word)
        {
            // Checking if the word ends with the letter 's'
            return word.EndsWith("s");
        }
    }
}

Sample Output:

Is 'Exercise' is plural? False
Is 'Exercises' is plural? True
Is 'Books' is plural? True
Is 'Book' is plural? False

Flowchart:

Flowchart: C# Sharp Exercises - Check whether a given word is plural or not.

Sample Solution-1:

C# Sharp Code:

using System;

namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            // Displaying whether 'Exercise' is considered plural by calling the test method
            Console.WriteLine("Is 'Exercise' plural? " + test("Exercise"));

            // Displaying whether 'Exercises' is considered plural by calling the test method
            Console.WriteLine("Is 'Exercises' plural? " + test("Exercises"));

            // Displaying whether 'Books' is considered plural by calling the test method
            Console.WriteLine("Is 'Books' plural? " + test("Books"));

            // Displaying whether 'Book' is considered plural by calling the test method
            Console.WriteLine("Is 'Book' plural? " + test("Book"));
        }

        // Method to test if a word is plural based on its ending
        public static bool test(string word)
        {
            int word_len = word.Length; // Getting the length of the input word
            return (word[word_len - 1] == 's') == true; // Checking if the last character of the word is 's'
        }
    }
}

Sample Output:

Is 'Exercise' is plural? False
Is 'Exercises' is plural? True
Is 'Books' is plural? True
Is 'Book' is plural? False

Flowchart:

Flowchart: C# Sharp Exercises - Check whether a given word is plural or not.

C# Sharp Code Editor:

Previous: Write a C# Sharp program which takes a positive number and return the nth odd number.
Next: Write a C# Sharp program to find sum of squares of elements of a given array of integers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/csharp-exercises/basic/csharp-basic-exercise-77.php