C# Code Interview: Count Words in String on CoderPad
Вставка
- Опубліковано 12 вер 2024
- In todays video we are going to go over a common code interview question that can come up which is simply to count the words in a string. We will be using C# as our language and CoderPad as our tool.
public static string StringCount(string s)
{
var sl = s.Split().ToList();
return sl.Count().ToString();
}
string myString = "Word1, Word2! Word3. Word4";
char[] delimiters = new char[] {' ', '
', '
'};
int numberWords = myString.Split(delimiters, StringSplitOptions.RemoveEmptyEntries).Length;
Console.WriteLine($"Number of Words {numberWords}");