//Program to demonstrate some for loops import java.util.Scanner; class SpaceCount { public static void main(String [] args) { Scanner input = new Scanner(System.in); /* This part counts the number of spaces in a piece of text. * We move through the string, reading one character at a time. * If the character we just read in happens to be a space, we increment a counter. * The method getChar(index), of the String class returns the character at the given index. * The method length(), of the String class, returns the length of the String. * If the length of a string is 'k', then the string has characters from index 0 to k-1 */ System.out.println("Enter your text: "); String junk=input.nextLine();//consume the preceding newline String text = input.nextLine(); int count=0; //counter. int length= text.length();// This Java method gets the length of the string /*strings are 0 indexed*/ for(int pos=0;pos