Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
Strings in Java
Problem Statement
Key Takeaways
Last Updated: Mar 27, 2024

Program to find all Punctuations in a String

Crack Google SDE interview : Essential projects
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM


Java is a widely-used programming language regarded as a favourite among programmers and is frequently ranked as the second most popular programming language in surveys.

Using Java programming language, we can perform many operations on Strings like counting white spaces, words in a string, Vowels, and consonants in a string. Calculating the total number of punctuations in a string can also be helpful.

 This article includes a brief introduction to String in Java and Java Program to find all punctuation characters in the String.

Strings in Java

Strings are a collection of characters that are frequently used in Java programming. Strings are objects in the Java programming language.

The String is a Java platform class that allows you to construct and manipulate strings.

The Java String class has many methods for working with strings, including compare()concat()equals()split()length()replace()compareTo()intern()substring(), and so on. Using these methods we can easily solve any problem related to Strings. We will use charAt() method in our java program to find number of punctuation characters.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Problem Statement

All of these 14 characters are considered punctuations in the English language:

  1. Period ( . )
  2. Question mark ( ? )
  3. The exclamation mark ( ! )
  4. Comma ( , )
  5. Semicolon ( ; )
  6. Colon ( : )
  7. Dash (   )
  8. Hyphen ()
  9. Parentheses ( () )
  10. Brackets ( [ ] )
  11. Braces ( { } )
  12. Apostrophe ( ' )
  13. Quotation Mark ( “ ” )
  14. Ellipsis ( ... )

We have to develop a Java program that accepts a string and counts the number of times these punctuations appear in the string before returning the count.


Char str[] = “ Good Morning! Join Coding Ninjas: Switch to Code.”


The total number of punctuation characters existing in the given string is 3

Also check out Addition of Two Numbers in Java here.


  1. Create a string or get a string from the user.
    In Java, There are two ways in which we can create a String object:

    By string literal:  We can create java String literal by using double quotes.
    For Example, String str = “Coding Ninjas”;  

    By new keyword: We can also create Java String by using the new keyword.
    For example, String str = new String(“Welcome”);  
    It creates two objects (in String pool and heap) and one reference variable where the variable ‘str’ will refer to the object in a heap.
  2. Declare and initialise a variable to count the number of punctuations.
  3. Now, use all the 14 punctuation marks to match each character of the given string if any character in the string matches one of the punctuation marks, the count variable increases by one.
  4. Finally, output the count variable, which contains the total amount of punctuation marks.


Let's put the above steps into action in Java code. The Filename of our Java program is

public class CodingNinjas   
public static void main (String args[])   
//count variable Stores the number of punctuation marks    
int count = 0;    
String str = "He said, 'Coding Ninjas is the best coding platform.' I have experienced it myself.";    
for (int i = 0; i < str.length(); i++)   
//matches each character and 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) == ':')   
System.out.println("The total number of punctuation characters existing in the given string is " +count);    


The total number of punctuation characters existing in the given string is 5


Practice it on online java compiler for better understanding.

Also check out String Args in Java


  1. What do you mean by a String pool in java?
    A string pool is a Java heap storage area where string literals are stored. String Intern Pool or String Constant Pool are other names for it. It's the same as object allocation. It is empty by default and is maintained privately by the Java String class.

  2. What is Java String charAt() Method?
    The charAt() method returns the character in a string at the provided index. The first character has an index of 0; the second has an index of 1, and so on.

  3. Is it possible to utilise String in a switch case?
    Java 7 included the ability to use Strings in switch cases, which was not possible in prior Java versions.
    Using Java 7 or above versions, you can utilise if-else conditions and switch cases to define conditional flow for Strings.

  4. Is it possible to convert String to Char?
    We can't convert a string to a single character because it's a series of characters. We can use the charAt() method to get the character at a particular index or use the toCharArray() method for converting a String to a character array.

  5. Is it possible to convert String to byte array?
    For converting a String to a byte array, use the String getBytes() method, and to restore a byte array to a String, use the String constructor new String(byte[] arr).

Key Takeaways

This article extensively discussed Important Points of String in Java and Java Program to find all punctuation characters in the String.

We hope that this blog has helped you enhance your knowledge regarding the Java Program for finding the total count of punctuation characters in a string. If you would like to learn more about Strings, check out our article on Strings in Java. . Do upvote our blog to help other ninjas grow.

Check out this article - Balanced Parentheses

Recommended problems -


To learn more about Java, take a look at various courses offered by Coding Ninjas. To know more about the best books of Java, check out our blog on 21 Best Java Programming Books for Beginners & Experts.

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more!!


We wish you Good Luck! Keep coding and keep reading Ninja!!


Previous article
Print Duplicate Elements in Array
Next article
Java program to find transpose of a matrix
Live masterclass