-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathCount_Words.java
35 lines (33 loc) · 889 Bytes
/
Count_Words.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* This is a java code of finding total number of
words in given string.
*/
import java.util.*;
import java.lang.*;
import java.io.*;
import java.lang.String;
public class count_words
{
public static int countWords(String str)
{
/* The java.util.StringTokenizer class allows
you to break a string into tokens.
*/
StringTokenizer tokens = new StringTokenizer(str);
return tokens.countTokens();
}
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter your string : ")
String str = scan.next();
System.out.println("No of words: " + countWords(str));
}
}
/*
Time complexity is O(n)
Space complexity is O(1)
Input :
Enter your string : I like to contribute here
Output:
No of words: 5
*/