I'm trying to make the genetive of the inserted name be decided automatically so that I won't have to manually insert the proper genetive for each string (in this case names)
As an example the genetive for James is ' and the genetive for Kennedy is 's.
I guess what I'm trying to say is that I want a cleaner implementation that allows me to skip having to write string Genetive:friend1(2..n) for each name
using System;
namespace Prac
{
class Program
{
static void Main(string[] args)
{
string Friend = ""; string last_char_Friend = ""; string Genetive_Friend = "";
string Friend1 = "Kennedy"; string last_char_Friend1 = Friend1[^1..]; string Genetive_Friend1 = "\'s";
string Friend2 = "James"; string last_char_Friend2 = Friend2[^1..]; string Genetive_Friend2 = "\'";
string Friend3 = "Ngolo Kante"; string last_char_Friend3 = Friend3[^1..]; string Genetive_Friend3 = "\'s";
Console.WriteLine($"My friends are named {Friend1}, {Friend2} and {Friend3}");
Console.WriteLine($"{Friend1}{Genetive_Friend1} name has {Friend1.Length} letters");
Console.WriteLine($"{Friend2}{Genetive_Friend2} name has {Friend2.Length} letters");
Console.WriteLine($"{Friend3}{Genetive_Friend3} name has {Friend3.Length} letters");
for (int i = 1; i < 4; i++)
{
Console.WriteLine($"{Friend + i}{Genetive_Friend + i} name has {(Friend + i).Length} letters");
}
Console.ReadLine();
}
}
}
There simply must be a smarter way for me to ensure that proper grammar is applied to each name, I've got a feeling that I can utilize reading the last char of the Friend string, but how do I within Console.WriteLine pick betweem ' and 's?
I'd like the for loop to print the same as the three individual Console.WriteLine lines.
Also this is my first time asking a question on Stackoverflow, please tell me if I've broken some unwritten rule on how questiosn should be formatted.
Theres a few questions here, firstly to loop through all the arguments you should create an Array (Or list or really anything extending IEnumerable) then you can iterate over it.
now following your example and not being particularly versed in grammar you can also write a method to check what is the last character of the input string and convert it into the genitive case
static void Main( string[] args )
{
string[] friends = new string[] { "Kennedy", "James", "Ngolo Kante" };
Console.WriteLine($"My friends are named {JoinEndingWithAnd(friends)}");
for ( int i = 1; i < friends.Length; i++ )
{
Console.WriteLine( $"{MakeGenitive(friends[i])} name has {friends[i].Length} letters" );
}
Console.ReadLine();
}
static string JoinEndingWithAnd(string[] friends)
{
string result = friends[0];
for ( int i = 1; i < friends.Length; i++ )
{
if ( i != friends.Length - 1)
{
result += $" , {friends[i]}";
}
else
{
result += $" and {friends[i]}";
}
}
return result;
}
static string MakeGenitive(string friend)
{
char lastLetter = friend[^1];
if( lastLetter == 's' )
{
return friend + "'";
}
return friend + "'s";
}