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

Java Program to count the total number of punctuation characters exist in a String

Example:

Input: str = "Hello Worlds !!.";
Output: 3

Approach

Java



public class PunctuationCheck {
    public static void main(String[] args) {
        int count = 0;
        String str = "Hello Worlds !!.";
        for (int i = 0; i < str.length(); i++) {
            // Checks whether given character is punctuation mark
            if (isPunctuation(str.charAt(i)))
                count++;
        }
        System.out.println("Total number of punctuations: " + count);

    }

    private static boolean isPunctuation(char ch) {
        if (ch == '!' || ch == ',' || ch == ';' || ch == '.' || ch == '?' 
            || ch == '-' || ch == '\'' || ch == '\"'
                || ch == ':')
            return true;
        return false;
    }
}


Read Interview Questions

Exception Handling Interview Questions

DBMS Interview Questions Set -1

DBMS Interview Questions Set -2

SQL Interview Question Set -1

SQL Interview Question Set -2

JPA Interview Questions Set -1

JPA Interview Question Set -2

Hibernate Interview Questions

Spring Boot Interview Questions Set 1

Spring Boot Interview Questions Set 2


No comments:

Post a Comment