Calculating string length in Java

Program Description

Program demonstrating use of string methods.

The program demonstrates accepting string from the user and calculating the length using the string class method length().

Program
/**********************************************
*  Calculating string length in Java
*	
*   code.cheraus.com
***********************************************/
import java.util.Scanner;

class String_Length {
                      public static void main(String args[])
                      {
                    	  Scanner str = new Scanner(System.in);
                    	  System.out.print("Enter the string : ");
						  
                    	  String s = str.nextLine();
						  
						  
                    	  int len = s.length();
                    	  System.out.println("The length of " + len);
                      }
}

Output
Test Case 1:


Enter the string : hello world
The length of 11

Test Case 2:


Enter the string : code.cheraus.com
The length of 16

Comments
comments powered by Disqus