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)