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