I am new to C# and coding in general and have a question on one exercise I'm doing. I am following the exercises on w3resource and I came across a challenge where I have to solve this: "Write a C# program to check if a given string contains 'w' character between 1 and 3 times."
My solution was this:
var theString = Console.ReadLine();
var theArray = theString.ToCharArray();
int betweenOneAndThree = 0;
for (int i = 0; i < theArray.Length - 1; i++)
{
if (theArray[i] == 'w')
betweenOneAndThree++;
}
Console.WriteLine(betweenOneAndThree >= 1 && betweenOneAndThree <= 3);
Console.ReadLine();
This worked just fine, but I checked their solution and it goes like this:
Console.Write("Input a string (contains at least one 'w' char) : ");
string str = Console.ReadLine();
var count = str.Count(s => s == 'w');
Console.WriteLine("Test the string contains 'w' character between 1 and 3 times: ");
Console.WriteLine(count >= 1 && count <= 3);
Console.ReadLine();
I can't see 's'
being declared as a char variable and I do not understand what it does here. Can anyone please explain to me what s => s == 'w'
does?
Yes, I have tried googling this. But I can't seem to find an answer.
Thank you in advance :)