Skip to content

Commit

Permalink
creating simple program in java to count character in string
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishakhasarode authored Oct 23, 2022
1 parent 200c851 commit 1be5ae8
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions java/Count Character in string.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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);
}
}

0 comments on commit 1be5ae8

Please sign in to comment.