Mohanapriya R Mohanapriya R
Updated date Apr 13, 2024
In this article, we will learn how to convert strings to pluralize in C#.
  • 1.3k
  • 0
  • 0

How to Convert Strings to Pluralize in C#?

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine(Pluralize("apple", 1));    // Output: "1 apple"
        Console.WriteLine(Pluralize("banana", 3));   // Output: "3 bananas"
        Console.WriteLine(Pluralize("cherry", 0));   // Output: "0 cherries"
    }

    static string Pluralize(string noun, int count)
    {
        if (count == 1)
            return $"{count} {noun}";
        else
            return $"{count} {noun}s";
    }
}

The Pluralize method takes a noun (string) and a count as parameters. It checks if the count is equal to 1, in which case it returns the singular form, otherwise, it appends an 's' to indicate plurality.

Output:

1 apple
3 bananas
0 cherries

 

Comments (0)

There are no comments. Be the first to comment!!!