C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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
Complexity:O(n) SolutionPython
count = 0;
str = "Good Morning! Mr. James Potter. Had your breakfast?";
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;
String str = "Good Morning! Mr. James Potter. Had your breakfast?";
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;
string str = "Good Morning! Mr. James Potter. Had your breakfast?";
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;
$str = "Good Morning! Mr. James Potter. Had your breakfast?";
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
|