Open In App

C# Program to Count Punctuation Characters in a String

Improve
Improve
Like Article
Like
Save
Share
Report

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:

 


Last Updated : 25 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads