Q:

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

0

Write a program in C# Sharp to create a function to input a string and count number of spaces are in the string.
Test Data :
Please input a string : This is a test string.
Expected Output :
"This is a test string." contains 4 spaces

All Answers

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

using System;
public class funcexer4
{
	public static int SpaceCount(string str)
	{   
		int spcctr=0;
		string str1;
		for (int i = 0;i < str.Length;i++)
			{
			  str1 = str.Substring(i,1);
			  if (str1 == " ")
					spcctr++;
			}
		return spcctr;
	}
public static void Main()
	{
	    string str2;
		Console.Write("\n\nFunction to count number of spaces in a string :\n");
        Console.Write("----------------------------------------------------\n");
		Console.Write("Please input a string : ");
        str2 = Console.ReadLine();
		Console.WriteLine("""+str2+"""+" contains {0} spaces", SpaceCount(str2) );
	}    
}

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

total answers (1)

Similar questions


need a help?


find thousands of online teachers now