Problem Statement
All of these 14 characters are considered punctuations in the English language:
- Period ( . )
- Question mark ( ? )
- The exclamation mark ( ! )
- Comma ( , )
- Semicolon ( ; )
- Colon ( : )
- Dash ( — )
- Hyphen ( - )
- Parentheses ( () )
- Brackets ( [ ] )
- Braces ( { } )
- Apostrophe ( ' )
- Quotation Mark ( “ ” )
- 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.
Input:
Char str[] = “ Good Morning! Join Coding Ninjas: Switch to Code.”
Output:
The total number of punctuation characters existing in the given string is 3
Also check out Addition of Two Numbers in Java here.
Algorithm
-
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.
-
Declare and initialise a variable to count the number of punctuations.
- 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.
- Finally, output the count variable, which contains the total amount of punctuation marks.
Implementation
Let's put the above steps into action in Java code. The Filename of our Java program is CodingNinjas.java
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) == ':')
{
count++;
}
}
System.out.println("The total number of punctuation characters existing in the given string is " +count);
}
}
Output:
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
FAQs
-
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.
-
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.
-
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.
-
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.
-
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!!