Key words
  • string-object.split
  • string-object.indexOf = Returns the location/number of the character in a word or string.
  • toCharArray();

import java.io.*;
import java.lang.String.*;
import java.lang.*;
//javac string_indexof.java
class string_indexof{
public static void main(String arguments[])throws IOException

{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
//needs throws IOException
System.out.print("Type some thing : ");
String str =br.readLine();
//String[] split(String whatever)
/* split , splits the sentence into arrays at the
occurrence of the string
*/

String[] words = str.split (" ");
int n1 = words.length;
// to detect the occurrences of a character
//indexOf(int ch)

int n4 = str.indexOf("");
System.out.println ("No of words are : " +n1);
char[] c_space = new char[n1];
char uc;
System.out.println("--for loop: ");
for (int i =0; i <= n1-1; i++)
{
c_space = words[i].toCharArray();
System.out.println(c_space);
System.out.println ("\t round " + i + " " + words[i]);
System.out.println (" indexof " + i + " " + words[n4 ]);
}//end for loop
System.out.println("--do while loop: ");
int x = 0;
do {
System.out.println ("\t " + x + " " + words[x]);
x++;

}while(x <= (n1-1));
}
}