Exercise
Function returning a value V2
Objetive
Write a C# program whose Main must be like this:
public static void Main()
{
__Console.WriteLine("\"Hello, how are you\" contains {0} spaces", ____CountSpaces("Hello, how are you") );
}
CountSpaces is a function that you must define and that will be called from inside Main.
As you can see in the example, it must accept an string as a parameter, and it must return an integer number (the amount of spaces in that string).
Example Code
using System;
class Program
{
public static int CountSpaces(string text)
{
int count = 0;
foreach (char c in text)
{
if (c == ' ')
{
count++;
}
}
return count;
}
public static void Main()
{
Console.WriteLine("\"Hello, how are you\" contains {0} spaces", CountSpaces("Hello, how are you"));
}
}