Skip to content
Related Articles

Related Articles

C# Program to Count Punctuation Characters in a String

View Discussion
Improve Article
Save Article
  • Last Updated : 25 Jul, 2022
View Discussion
Improve Article
Save Article

C# is a general-purpose, modern and object-oriented programming language pronounced as “C Sharp”. In this article we will see the C# program, to count punctuation characters in a given string. 

Algorithm:

  1. First, create a string or get the string from the user.
  2. Declare a variable to count the number of punctuations. If any character is matched with the punctuations, increment the count by 1.
  3. Print the count.

Example:

C#




// C# program for count punctuation 
// characters in a given string
using System;
  
namespace Count_Punctuation_characters_in_a_given_string
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a string:");
            string input = Console.ReadLine();
            int count = 0;
            for(int i=0;i<input.Length;i++)
            {
                if(input[i]=='!'|| input[i]==';'|| input[i]==','|| 
                   input[i]==':'|| input[i]=='?'|| input[i]=='.')
                {
                    count++;
                }
            }
            Console.WriteLine($"No of punctuations are:{count}");
        }
    }
}

Output:

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!