MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / CountWords

Class CountWords

Java/String/CountWords.java:6–25  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4import java.util.Scanner;
5
6public class CountWords {
7
8 public static int countWords(String str ) {
9 int count = 0;
10 for (int i = 0; i < str.length(); i++) {
11 if (str.charAt(i) == ' ') {
12 count += 1;
13 }
14 }
15 return count + 1;
16 }
17
18 public static void main(String[] args) {
19 Scanner sc = new Scanner(System.in);
20 String str = sc.nextLine();
21
22 int n = countWords(str);
23 System.out.println(n);
24 }
25}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected