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

Introduction

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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();
}
}
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(); } }
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

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

Leave a Comment