TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

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.

<< Back to PROGRAM

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.

Complexity:

O(n)

Solution

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

Next TopicPrograms List




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf