Java Program to Count the Total Number of Punctuation Characters Exists in a StringIn the previous Java string programs, we have counted the number of words, white spaces, vowels, consonants, and characters. Sometimes it also becomes necessary to count the total number of punctuations in a string. So, in this section, we are going to create a Java program that counts the total number of punctuations in a given string. Steps to Count the Punctuations
Let's implement the above steps in a Java program. CountPunctuation.java Output: The number of punctuations exists in the string is: 5 In the above program, we have used a for loop that iterates over the string and executes till the length of the string. For determining the position of the characters in a string, we have used the charAt() method that returns the character at the specified index in a string. For each iteration, each and every character is matched with the punctuation marks by using the == operator. If any character is matched with a punctuation mark the count variable is incremented by 1. At last, we have printed the value of the count variable. Next TopicJava Programs |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India