- Course
- Functions A
- Function count spaces of a text
Function count spaces of a text
Last updated:
8/23/2020
⁃
Difficulty:
Intermediate
Create a program in C# that implements a function called CountSpaces that receives as a parameter a text requested from the user. Then show the total spaces that the text has.
Input
Hello, how are you?
Output
3
Solution
using System;
public class FunctionCountSpaces
{
public static void Main(string[] args)
{
string text = Console.ReadLine();
int countSpaces = CountSpaces(text);
Console.WriteLine(countSpaces);
}
public static int CountSpaces(string text)
{
int count = 0;
string letter;
for (int i = 0; i < text.Length; i++)
{
letter = text.Substring(i, 1);
if (letter == " ")
{
count++;
}
}
return count;
}
}