Ask Question
9 October, 03:30

Write a function listLengthOfAllWords which takes in an array of words (strings), and returns an array of numbers representing the length of each word.

+4
Answers (1)
  1. 9 October, 06:41
    0
    public static int[] listLengthOfAllWords (String [] wordArray) {

    int[] intArray = new int[wordArray. length];

    for (int i=0; i
    int lenOfWord = wordArray[i]. length ();

    intArray[i]=lenOfWord;

    }

    return intArray;

    }

    Explanation:

    Declare the method to return an array of ints and accept an array of string as a parameter within the method declare an array of integers with same length as the string array received as a parameter. Iterate using for loop over the array of string and extract the length of each word using this statement int lenOfWord = wordArray[i]. length (); Assign the length of each word in the String array to the new Integer array with this statement intArray[i]=lenOfWord; Return the Integer Array

    A Complete Java program with a call to the method is given below

    import java. util. Arrays;

    import java. util. Scanner;

    public class ANot {

    public static void main (String[] args) {

    String []wordArray = {"John", "James", "David", "Peter", "Davidson"};

    System. out. println (Arrays. toString (listLengthOfAllWords (wordArray)));

    }

    public static int[] listLengthOfAllWords (String [] wordArray) {

    int[] intArray = new int[wordArray. length];

    for (int i=0; i
    int lenOfWord = wordArray[i]. length ();

    intArray[i]=lenOfWord;

    }

    return intArray;

    }

    }

    This program gives the following array as output: [4, 5, 5, 5, 8]
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a function listLengthOfAllWords which takes in an array of words (strings), and returns an array of numbers representing the length ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers