Function Returning A Value V2 - C# Programming Exercise

In this C# exercise, you are asked to write a program where the Main method must be like this:


public static void Main()
{
Console.WriteLine("\"Hello, how are you\" contains {0} spaces", CountSpaces("Hello, how are you") );
}

The goal of this exercise is to define the CountSpaces function, which must accept a string parameter (in this case, the string "Hello, how are you") and return an int value, which will be the number of spaces in that string. This exercise will help you practice string handling and counting specific characters within them in C#. You need to understand how to pass parameters to functions and how to return a value from them.

 Category

Functions

 Exercise

Function Returning A Value V2

 Objective

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).

 Write Your C# Exercise

// Importing the System namespace to access basic system functions
using System;

class Program
{
    // Function to count the number of spaces in a given string
    public static int CountSpaces(string text)
    {
        // Initialize a counter for spaces
        int count = 0;

        // Loop through each character in the string
        foreach (char c in text)
        {
            // If the character is a space, increment the counter
            if (c == ' ')
            {
                count++;
            }
        }

        // Return the total number of spaces found in the string
        return count;
    }

    // Main method to call the CountSpaces function and display the result
    public static void Main()
    {
        // Call the CountSpaces function with a sample string, and display the result
        // The string "Hello, how are you" contains 4 spaces, so it will display: "Hello, how are you contains 4 spaces"
        Console.WriteLine("\"Hello, how are you\" contains {0} spaces", CountSpaces("Hello, how are you"));
    }
}

 Share this C# exercise

 More C# Programming Exercises of Functions

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Function write centered

    In this C# exercise, you are asked to write a function that displays the given text centered on the screen (assuming a screen width of 80 characters). The code should...

  •  Function write underlined

    In this C# exercise, you are asked to write a function that displays the given text centered on the screen (assuming a screen width of 80 characters) and then underli...

  •  Function sum of array

    In this C# exercise, you are asked to write a program to calculate the sum of the elements in an array. The Main function should look like the following:<...

  •  Function double

    In this C# exercise, you are asked to write a function named "Double" to calculate and return an integer number doubled. For example, when calling the Double(7...

  •  Function Double reference parameter

    In this C# exercise, you are asked to write a function named "Double" that calculates the double of an integer number and modifies the data passed as an argument. Thi...

  •  Function swap reference parameters

    In this C# exercise, you are asked to write a function named "Swap" that swaps the values of two integer numbers, which are passed by reference. This is done b...