Write a java program to count words in a string.

Write a program to read a string and count the number of words present in it.

The return type is the integer giving out the count of words.
Input and Output Format:
Input consists of a string.
The output consists of the integer.
Refer sample output for formatting specifications.

Sample Input 1:
Today is Sunday
Sample Output 1:
3

Count words in a string in Java.

import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    String s1 = sc.nextLine();
    StringTokenizer st = new StringTokenizer(s1, " ");
    int n = st.countTokens();
    System.out.println(n);
  }

}

Write a java program to count elements in a String array.

Given a string array (s) and non-negative integer (n) and return the number of elements in the array which have the same number of characters as the given in N.

The return type is the string formed based on rules.

Input and Output Format:
Input consists of an integer indicating the number of elements in the string array followed the elements and ended by the non-negative integer (N).
The output consists of an integer.
Refer sample output for formatting specifications.

Sample Input 1:
4
a
bb
b
ccc
1
Sample Output 1:
2

Sample Input 2:
5
dog
cat
monkey
bear
fox
3
Sample Output 2:
3

import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    int n = sc.nextInt();
    String str[] = new String[n];
    for (int i = 0; i < n; i++) {
      str[i] = sc.next();
    }
    int c = sc.nextInt();

    System.out.println(display(n, str, c));
  }

  public static int display(int n, String str[], int c) {
    int count = 0;
    for (int i = 0; i < str.length; i++) {
      if (str[i].length() == c) {
        count++;
      }
    }
    return count;
  }

}