Alphabet

Java program: count the total number of characters including space in the given string

Java program to count the total number of characters including space in the given string

In this article, we will discuss the concept of the Java program to count the total number of characters including space in the given string

In this post, we are going to learn how to count the total number of character including space of the given String in Java programming language

Count character

Java program to count to the total number of characters in the given string

Code to count the total number of characters in the given string using for loop

The program allows the user to enter a String and then it counts and display the total number of characters including space of the given string using for loop in Java programing language

Program 1

import java.util.Scanner;
public class Count_WordOfString{
public static void main(String args[]){
String str; //variable declaration
int count=0;

 Scanner scan=new Scanner(System.in); 
    //create a scanner object for input
    
System.out.print("Enter the String for count characters \n ");
str=scan.nextLine();

//count each character without space
for(int i=0; i<str.length(); i++){
    if(str.charAt(i)!='\0'){
        count++;
    }
}
//display the total number of characters in te given string
System.out.print("Total number of character in a string: "+count);
}
}

When the above code is executed, it produces the following result

Enter the String for count characters
Java Language
Total number of character in a string:13

 

Approach

  1. Declare a String variable as String str;
  2. Declare and initialize an integer variable as int count=0;
  3. The user asked to enter a string to count character
  4. The entered string is stored in the str variable
  5. A for-loop is used to count total characters of the given string using the count variable
  6. It is initialized as i=0, checks the condition whether i<str.length(); and executes the loop until the given condition becomes true
  7. Use an if condition to test if(str.charAt(i)!=’ \0′), if it is true,  count  becomes count + 1(count=count+1)
  8. Finally, the program displays the total number of the character using the count variable

Code to count the total number of characters in the given string using while loop

The program allows the user to enter a String and then it counts and display the total number of characters including space of the given string using while loop in Java programing language

Program 2

import java.util.Scanner;
public class Count_WordOfString1{
public static void main(String args[]){
String str; //variable declaration
int count=0;

 Scanner scan=new Scanner(System.in); 
    //create a scanner object for input
    
System.out.println("Enter the String for count characters  ");
str=scan.nextLine();

//count each character without space
int i=0;
while(i<str.length()){
    if(str.charAt(i)!='\0'){
        count++;
    }
     i++;
}
//display the total number of characters in te given string
System.out.print("Total number of character in a string:"+count);
}
}

When the above code is executed, it produces the following result

Enter the String for count characters
Java Program
Total number of character in a string:12


Approach

  1. Declare a String variable as String str;
  2. Declare and initialize an integer variable as int count=0;
  3. The user asked to enter a string to count character
  4. The entered string is stored in the str variable
  5. A while-loop is used to count total characters of the given string using the count variable
  6. It is initialized as i=0, checks the condition whether i<str.length(); and executes the loop until the given condition becomes true
  7. Use an if condition to test if(str.charAt(i)!=’ \0′), if it is true,  count  becomes count + 1(count=count+1)
  8. Finally, the program displays the total number of the character using the count variable

Code to count the total number of characters in the given string using do-while loop

The program allows the user to enter a String and then it counts and display the number of characters including space of the given string using do-while loop in Java programing language

Program 3

import java.util.Scanner;
public class Count_WordOfString2{
public static void main(String args[]){
String str; //variable declaration
int count=0;

 Scanner scan=new Scanner(System.in); 
    //create a scanner object for input
    
System.out.println("Enter the String for count characters  ");
str=scan.nextLine();

//count each character without space
int i=0;
do{
    if(str.charAt(i)!='\0'){
        count++;
    }
     i++;
}while(i<str.length());
//display the total number of characters in te given string
System.out.print("Total number of character in a string:"+count);
}
}

When the above code is executed, it produces the following result

Enter the String for count characters
Java OOP Language
Total number of character in a string:17

 

Approach

  1. Declare a String variable as String str;
  2. Declare and initialize an integer variable as int count=0;
  3. The user asked to enter a string to count character
  4. The given string is stored in the variable str;
  5. A do-while-loop is used to count total characters of the given string using the count variable
  6. It is initialized as i=0, checks the condition whether i<str.length(); and executes the loop until the given condition becomes true
  7. Use an if condition to test if(str.charAt(i)!=’ \0′), if it is true,  count  becomes count + 1(count=count+1)
  8. Finally, the program displays the total number of the character using the count variable

 

Suggested for you

for loop in Java language

while loop in Java language

do-while loop in Java language

 

Similar post

C++ program to count the total number of characters in the given string

C program to count the total number of characters in the given string

Python program to count the total number of characters in the given string

C program to count the total number of characters in the given string including space

C++ program to count the total number of characters in the given string including space

Python program to count the total number of characters in the given string including space

 

C program to Count the total number of characters in the given string including space
C++ program to count the total number of characters in the given string including space
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago