Java Program to count the total number of characters in a string
Example:
Input: str = " Hello World 12! ";
Output: 13
Approach: Count all character excluding while space
Java
public class CountTotalCharacter {public static void main(String[] args) {String str = " Hello World 12! ";int count = 0;for (int i = 0; i < str.length(); i++) {if (str.charAt(i) != ' ') {count++;}}System.out.println(count);}}
Approach: Count the only character
Java
public class CountTotalCharacter {public static void main(String[] args) {String str = " Hello World 12! ";int count = 0;for (int i = 0; i < str.length(); i++) {if (Character.isAlphabetic(str.charAt(i))) {count++;}}System.out.println(count);}}
No comments:
Post a Comment