import java.util.Scanner;
import java.util.StringTokenizer;
public class CountTokens
{
public static void main(String[] args)
{
//create StringTokenizer object
String S;
Scanner scan = new Scanner (System.in);
// enter your string here.
System.out.print("Enter the string : ");
// will read string and store it in "S" for further process.
S = scan.nextLine();
StringTokenizer st = new StringTokenizer(S, " ");
// search for token while the string ends.
while(st.hasMoreTokens())
{
// print all the tokens.
System.out.println("Remaining are : " + st.countTokens());
System.out.println(st.nextToken());
}
}
}
Output
Enter the string : We are $ {} 7 ? here.
Remaining are : 7
We
Remaining are : 6
are
Remaining are : 5
$
Remaining are : 4
{}
Remaining are : 3
7
Remaining are : 2
?
Remaining are : 1
here.
Program
Output