forked from codehouseindia/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathCountCharacter
More file actions
15 lines (13 loc) · 423 Bytes
/
CountCharacter
File metadata and controls
15 lines (13 loc) · 423 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class CountCharacter {
public static void main(String[] args) {
String string = "The best of both worlds";
int count = 0;
// Counts each character except space
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) != ' ')
count++;
}
// Displays the total number of characters present in the given string
System.out.println("Total number of characters in a string: " + count);
}
}