Java program to print the length of all strings from a given array
GD.java
class GD
{
    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
godarda@gd:~$ javac GD.java
godarda@gd:~$ java GD ——————————————————————————————————————————— 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 ——————————————————————————————————————————— godarda@gd:~$
Comments and Reactions