using System;
public class Test
{
static int GetWordCount(string text)
{
int wordCount = 0, index = 0;
// skip whitespace until first word
while (index < text.Length && char.IsWhiteSpace(text[index]))
index++;
while (index < text.Length)
{
// check if current char is part of a word
while (index < text.Length && !char.IsWhiteSpace(text[index]))
index++;
wordCount++;
// skip whitespace until next word
while (index < text.Length && char.IsWhiteSpace(text[index]))
index++;
}
return wordCount;
}
private static void AssertWordCount(int expectedCount, string input)
{
var actual = GetWordCount(input);
if (actual != expectedCount)
throw new Exception(string.Format("Input '{0}': expected {1}, but got {2}",
input, expectedCount, actual));
}
public static void Main()
{
foreach (var test in new[] { "", " ", " " })
{
AssertWordCount(0, test);
}
foreach (var test in new[] { "test", " test", "test ", " test " })
{
AssertWordCount(1, test);
}
foreach (var test in new[] { "a b", " a b", "a b ", " a b " })
{
AssertWordCount(2, test);
}
Console.WriteLine("Passed without exceptions");
}
}
dXNpbmcgU3lzdGVtOwoKcHVibGljIGNsYXNzIFRlc3QKewogICAgc3RhdGljIGludCBHZXRXb3JkQ291bnQoc3RyaW5nIHRleHQpCiAgICB7CiAgICAgICAgaW50IHdvcmRDb3VudCA9IDAsIGluZGV4ID0gMDsKCiAgICAgICAgLy8gc2tpcCB3aGl0ZXNwYWNlIHVudGlsIGZpcnN0IHdvcmQKICAgICAgICB3aGlsZSAoaW5kZXggPCB0ZXh0Lkxlbmd0aCAmJiBjaGFyLklzV2hpdGVTcGFjZSh0ZXh0W2luZGV4XSkpCiAgICAgICAgICAgIGluZGV4Kys7CgogICAgICAgIHdoaWxlIChpbmRleCA8IHRleHQuTGVuZ3RoKQogICAgICAgIHsKICAgICAgICAgICAgLy8gY2hlY2sgaWYgY3VycmVudCBjaGFyIGlzIHBhcnQgb2YgYSB3b3JkCiAgICAgICAgICAgIHdoaWxlIChpbmRleCA8IHRleHQuTGVuZ3RoICYmICFjaGFyLklzV2hpdGVTcGFjZSh0ZXh0W2luZGV4XSkpCiAgICAgICAgICAgICAgICBpbmRleCsrOwoKICAgICAgICAgICAgd29yZENvdW50Kys7CgogICAgICAgICAgICAvLyBza2lwIHdoaXRlc3BhY2UgdW50aWwgbmV4dCB3b3JkCiAgICAgICAgICAgIHdoaWxlIChpbmRleCA8IHRleHQuTGVuZ3RoICYmIGNoYXIuSXNXaGl0ZVNwYWNlKHRleHRbaW5kZXhdKSkKICAgICAgICAgICAgICAgIGluZGV4Kys7CiAgICAgICAgfQoKICAgICAgICByZXR1cm4gd29yZENvdW50OwogICAgfQoKICAgIHByaXZhdGUgc3RhdGljIHZvaWQgQXNzZXJ0V29yZENvdW50KGludCBleHBlY3RlZENvdW50LCBzdHJpbmcgaW5wdXQpCiAgICB7CiAgICAgICAgdmFyIGFjdHVhbCA9IEdldFdvcmRDb3VudChpbnB1dCk7CiAgICAgICAgaWYgKGFjdHVhbCAhPSBleHBlY3RlZENvdW50KQogICAgICAgICAgICB0aHJvdyBuZXcgRXhjZXB0aW9uKHN0cmluZy5Gb3JtYXQoIklucHV0ICd7MH0nOiBleHBlY3RlZCB7MX0sIGJ1dCBnb3QgezJ9IiwKICAgICAgICAgICAgICAgIGlucHV0LCBleHBlY3RlZENvdW50LCBhY3R1YWwpKTsKICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgTWFpbigpCiAgICB7CiAgICAgICAgZm9yZWFjaCAodmFyIHRlc3QgaW4gbmV3W10geyAiIiwgIiAiLCAiICAgIiB9KQogICAgICAgIHsKICAgICAgICAgICAgQXNzZXJ0V29yZENvdW50KDAsIHRlc3QpOwogICAgICAgIH0KCiAgICAgICAgZm9yZWFjaCAodmFyIHRlc3QgaW4gbmV3W10geyAidGVzdCIsICIgdGVzdCIsICJ0ZXN0ICIsICIgdGVzdCAiIH0pCiAgICAgICAgewogICAgICAgICAgICBBc3NlcnRXb3JkQ291bnQoMSwgdGVzdCk7CiAgICAgICAgfQoKICAgICAgICBmb3JlYWNoICh2YXIgdGVzdCBpbiBuZXdbXSB7ICJhIGIiLCAiIGEgYiIsICJhIGIgIiwgIiBhIGIgIiB9KQogICAgICAgIHsKICAgICAgICAgICAgQXNzZXJ0V29yZENvdW50KDIsIHRlc3QpOwogICAgICAgIH0KCiAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoIlBhc3NlZCB3aXRob3V0IGV4Y2VwdGlvbnMiKTsKICAgIH0KfQ==