Java program to print the length of all strings from a given array
KW.java
class KW
{
    public static void main(String args[])
    {
        System.out.println("———————————————————————————————————————————");
        System.out.println("Program to print strings and its length");
        System.out.println("———————————————————————————————————————————");
        String s[]={"Mouse","Keyboard","Processor","Pen Drive","Joystick"};
        System.out.println("Array contains the following strings"+"\n{");
        
        for(String arr:s)
        System.out.println("   "+arr);
        System.out.println("}\n");
        
        for(int i=0;i<s.length;i++)
        System.out.println(s[i]+" "+s[i].length());
        
        System.out.println("———————————————————————————————————————————");
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW ——————————————————————————————————————————— Program to print strings and its length ——————————————————————————————————————————— Array contains the following strings { Mouse Keyboard Processor Pen Drive Joystick } Mouse 5 Keyboard 8 Processor 9 Pen Drive 9 Joystick 8 ——————————————————————————————————————————— kodingwindow@kw:~$
Advertisement