Q:

Write a C# program to create a function to input a string and count number of spaces are in the string

0

Write a C# program to create a function to input a string and count number of spaces are in the string

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

internal class Program
{
    public static int Countspaces(string str)
    {
        int spcctr = 0;
        string strone;
        for (int i = 0; i < str.Length; i++)
        {
            strone = str.Substring(i, 1);
            if (strone == " ")
                spcctr++;
        }
        return spcctr;
    }
    public static void Main()
    {
        string strtwo;
        Console.Write("Please Enter a string : ");
        strtwo = Console.ReadLine();
        Console.WriteLine(""" + strtwo + """ + " contains {0} spaces", Countspaces(strtwo));

        Console.ReadLine();
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer
need an explanation for this answer? contact us directly to get an explanation for this answer

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..

using System;
 
public class functionexcercise
{
    public static int Countspaces(string str)
    {
        int spcctr = 0;
        string strone;
        for (int i = 0; i < str.Length; i++)
        {
            strone = str.Substring(i, 1);
            if (strone == " ")
                spcctr++;
        }
        return spcctr;
    }
    public static void Main()
    {
        string strtwo;        
        Console.Write("Please Enter a string : ");
        strtwo = Console.ReadLine();
        Console.WriteLine(""" + strtwo + """ + " contains {0} spaces", Countspaces(strtwo));
 
        Console.ReadLine();
    }
}

Result:

Please Enter a string : techstudy - the complete debuggin solution

"techstudy - the complete debuggin solution" contains 5 spaces

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (2)

Write a C# program to create a function to calcula... >>
<< Write a C# program to add two numbers using functi...