15. Program to count the total number of punctuation characters exists in a String
class Main{
static int countPunctuation(String str){
int count = 0, len = str.length();
for(int i = 0; i < len; i++){
char ch = str.charAt(i);
if(ch == '!' || ch == ',' || ch == '.' || ch == '?' || ch == '!' || ch == ':' || ch == ';' || ch == ''' || ch == '"' || ch == '-'){
count++;
}
}
return count;
}
public static void main(String[] args){
int count = countPunctuation("Oh ! I love you. Uhh-hh");
System.out.println("Number of Punctuations in string is : " + count);
}
}
OUTPUT
Number of Punctuations in string is : 3