50.4k views
5 votes
8. Given the array String[] words, which already contains 1 or more values, write a block of code which counts and returns the number of words which start with the letter k

1 Answer

12 votes

Answer:

Following are the code to this question:

public class Main//defining a class

{

public static void main(String[] arg)//defining main method

{

String[] words={"Key","day", "Know", "kind"};//defining array of String words

int x=0;//defining integer variable for count value

for(int i=0;i<words.length;i++)//defining for loop for count value

System.out.print("The number of letters which starts from k is: "+ x);//print value with message

}

}

Output:

The number of letters which starts from k is: 3

Step-by-step explanation:

In this code, inside the main method an array of String "words" is defined that hold a value, and in the next step an integer variable "x" is defined, which is used to count the letters, which starts from k.

For this, a for loop is used that counts the value and in this an, if block is defined that uses the "startsWith" method to check the value and increment the value of x and at the last, it prints its value.

User Covati
by
6.5k points