C# Program to Count Punctuation Characters in a String
Last Updated :
25 Jul, 2022
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:
- First, create a string or get the string from the user.
- Declare a variable to count the number of punctuations. If any character is matched with the punctuations, increment the count by 1.
- Print the count.
Example:
C#
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:
Share your thoughts in the comments
Please Login to comment...