Q:

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

0

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.

Complexity:

O(n)

Input:

char str [] = "Good Morning! Mr. James Potter. Had your breakfast?"  

Output:

If any character in the string is matched with ('!', "," ,"\'" ,";" ,"\"", ".", "-" ,"?"), increment the count by 1.

Total number of punctuation characters exists in string: 4

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions