# Program to Count the Total Number of Punctuation Characters Exists in a String

Program to Count the Total Number of Punctuation Characters Exists in a String on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

## Q. Program to count the total number of punctuation characters exists in a string.

In this program, all the subsets of the string need to be printed. The subset of a string is the character or the group of characters that are present inside the string. For example, all possible subsets of a string "FUN" will be F, U, N, FU, UN, FUN.

### Algorithm

1. Define a string.
2. If any character in the string is matched with (! , . , ' , - , " , ? , ; ) , increment the count by 1.
3. Print the count.

O(n)

## Python

```count = 0;
for i in range (0, len (str)):
#Checks whether given character is a punctuation mark
if str[i] in ('!', "," ,"\'" ,";" ,"\"", ".", "-" ,"?"):
count = count + 1;

print ("Total number of punctuation characters exists in string: ");
print (count);
```

Output:

```Total number of punctuation characters exists in string:
4
```

## C

```#include <stdio.h>
int main ()
{
int i, countPuncMarks = 0;
char str [] = "Good Morning! Mr. James Potter. Had your breakfast?";

//Checks whether given character is punctuation mark
for(i = 0; i < strlen(str); i++){
if(str[i] == '!' || str[i] == ',' || str[i] == ';' || str[i] == '.' || str[i] == '?' ||
str[i] == '-' || str[i] == '\'' || str[i] == '\"' || str[i] == ':') {
countPuncMarks++;
}
}
printf("Total number of punctuation characters exists in string : %d ",countPuncMarks);
return 0;
}
```

Output:

```Total number of punctuation characters exists in string: 4
```

## JAVA

```public class punctuation {
public static void main (String [] args) {
//Stores the count of punctuation marks
int countPuncMarks = 0;
for (int i = 0; i < str.length(); i++) {
//Checks whether given character is punctuation mark
if(str.charAt(i) == '!' || str.charAt(i) == ',' || str.charAt(i) == ';' || str.charAt(i) == '.' ||        str.charAt(i) == '?' || str.charAt(i) == '-' ||
str.charAt(i) == '\'' || str.charAt(i) == '\"' || str.charAt(i) == ':') {
countPuncMarks++;
}
}
System.out.println("Total number of punctuation characters exists in string: " +        countPuncMarks);
}
}
```

Output:

```Total number of punctuation characters exists in string: 4
```

## C#

```using System;
public class Program
{
public static void Main ()
{
//Stores the count of punctuation marks
int countPuncMarks = 0;
for (int i = 0; i < str.Length; i++) {
//Checks whether given character is punctuation mark
if(str[i] == '!' || str[i] == ',' || str[i] == ';' || str[i] == '.' || str[i] == '?' || str[i] == '-' ||
str[i] == '\'' || str[i] == '\"' || str[i] == ':') {
countPuncMarks++;
}
}
Console.WriteLine("Total number of punctuation characters exists in string: " + countPuncMarks);
}
}
```

Output:

```Total number of punctuation characters exists in string: 4
```

## PHP

```<!DOCTYPE html>
<html>
<body>
<?php
//Stores the count of punctuation marks
\$count = 0;
for(\$i = 0; \$i < strlen(\$str); \$i++) {
//Checks whether given character is punctuation mark
if(\$str[\$i] == '!' || \$str[\$i] == ',' || \$str[\$i] ==     ';' || \$str[\$i] == '.'  || \$str[\$i] == '?' || \$str[\$i] == '-' || \$str[\$i] == '\'' || \$str[\$i] ==   '\"' || \$str[\$i] == ':') {
\$count++;
}
}
echo "Total number of punctuation characters exists in string : ";
echo \$count;
?>
</body>
</html>
```

Output:

```Total number of punctuation characters exists in string: 4
```

Next TopicPrograms List