Tech Study

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

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

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

Python Examples

Introduction: Python Examples are the basic programming concepts of python like python syntax,python data types,,python operators,python if else,python comments etc.. …

Read more

C String Functions

C String Functions perform certain operations, It provides many useful string functions which can come into action. The <string.h> header …

Read more