Java program to find the length of a given string
KW.java
import java.util.Scanner;
class KW
{
    public static void main(String args[])
    {
        String s;
        System.out.println("———————————————————————————————————————————");
        System.out.println("Program to find the length of a given string");
        System.out.println("———————————————————————————————————————————");
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the string ");
        s=sc.nextLine();
        System.out.println("\nEntered string is "+s);
        int l=s.length();
        System.out.println("\nThe length of ("+s +") = "+l);
        System.out.println("———————————————————————————————————————————");
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW ——————————————————————————————————————————— Program to find the length of a given string ——————————————————————————————————————————— Enter the string Hello, World! Entered string is Hello, World! The length of (Hello, World!) = 13 ——————————————————————————————————————————— kodingwindow@kw:~$
Java program to find the length of a given string
KW.java
class KW
{
    public static void main(String args[])
    {
        String s1="KODINGWINDOW";
        int length=0;
        for(String s2:s1.split(""))
        {
            length++;
        }
        System.out.println("Length of a string is: "+length);  
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW Length of a string is: 12 kodingwindow@kw:~$
Advertisement