Saved Bookmarks
| 1. |
Write a program to calculate the total number of characters in the String? |
|
Answer» To FIND the total count of characters in a specified STRING, we can use the for loop, while loop, or do while loop. Below is a Java program to calculate the total number of characters in a string using for loop. public class TotatCharacters{ public static void main(String[] args) { String str = "Scaler by InterviewBit"; int count = 0; System.out.println("Input String: "+str); //Count total characters in the given string except space for(int i = 0; i < str.length(); i++) { if(str.charAt(i) != ' ') count++; } //Display total number of characters in the given string System.out.println("The total number of characters in the given string: " + count); } }Output: Input String: Scaler by InterviewBitThe total number of characters in the given string: 20 |
|