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


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. using System;
  2.  
  3. public class functionexcercise
  4. {
  5. public static int Countspaces(string str)
  6. {
  7. int spcctr = 0;
  8. string strone;
  9. for (int i = 0; i < str.Length; i++)
  10. {
  11. strone = str.Substring(i, 1);
  12. if (strone == " ")
  13. spcctr++;
  14. }
  15. return spcctr;
  16. }
  17. public static void Main()
  18. {
  19. string strtwo;
  20. Console.Write("Please Enter a string : ");
  21. strtwo = Console.ReadLine();
  22. Console.WriteLine("\"" + strtwo + "\"" + " contains {0} spaces", Countspaces(strtwo));
  23.  
  24. Console.ReadLine();
  25. }
  26. }

Result

C# language techstudy

     





Related Topics




Popular Posts