importjava.util.Scanner;classGD{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
godarda@gd:~$ javac GD.java godarda@gd:~$ java GD
———————————————————————————————————————————
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
———————————————————————————————————————————
godarda@gd:~$
Java program to find the length of a given string
GD.java
classGD{publicstaticvoidmain(Stringargs[]){Strings1="KODINGWINDOW";intlength=0;for(Strings2:s1.split("")){length++;}System.out.println("Length of a string is: "+length);}}
Output
godarda@gd:~$ javac GD.java godarda@gd:~$ java GD
Length of a string is: 12
godarda@gd:~$
Dear User, Thank you for visitng GoDarda. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.