How to count the number of spaces and characters in a string with C#

1 Answer

0 votes
using System;

class Program
{
    static void Main() {
        string s = "c # . n e t";
        int spaceCount = 0, charCount = 0;

        foreach (char ch in s) {
            if (ch ==  ' ')
                spaceCount += 1;
            else
                charCount += 1;
        }

        Console.WriteLine(spaceCount);
        Console.WriteLine(charCount);
    }
}




/*
run:
  
5
6
  
*/

 



answered Sep 28, 2021 by avibootz
edited Sep 28, 2021 by avibootz
...