[Contains Spoilers]
Hey, im playing Code Hunt using C#. Most of the questions are fairly simple and straight forward. But this one is driving me nuts. The question is to count the number of occurrences of a's (Question 2.06) or of a given character (Question 2.07).
The obvious solution gives only two points.
int counter = 0;
foreach (var c in str)
{
if (c == 'a') counter ++;
}
return counter;
The shorter version using linq gives also only two points.
return s.Count(c=>c=='a');
Any ideas for different solving approaches. I can't think of any simpler or shorter solution to this problem.
For the ones who do not know Code Hunt. It is programming game, which gives points on how elegant a solution to given problem is. The simplest and best solution gets 3 Points. Thank You and have a nice day ;)
Edit: Bashers answer:
s.Split('a').Length - 1;
gives also just two points.