importjava.util.Scanner;classKW{publicstaticvoidmain(Stringargs[]){Strings;System.out.println("———————————————————————————————————————————");System.out.println("Program to find the length of a given string");System.out.println("———————————————————————————————————————————");Scannersc=newScanner(System.in);System.out.print("Enter the string ");s=sc.nextLine();System.out.println("\nEntered string is "+s);intl=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
classKW{publicstaticvoidmain(Stringargs[]){Strings1="KODINGWINDOW";intlength=0;for(Strings2: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:~$
Dear User, Thank you for visitng KodingWindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.