public static void main(String[] args)
{
String sentence;
int wordCount = 0;
char prevChar = ' ';
char currentChar;
int index;
System.out.println("Enter a sentence: ");
sentence = EasyIn.getString();
for (index = 0; index < sentence.length(); index++)
{
currentChar = sentence.charAt(index);
if (Character.isLetter(currentChar) && !Character.isLetter(prevChar))
{
wordCount++;
}
prevChar = currentChar;
}
System.out.println("The number of words is " + wordCount + ".");
}
{
String sentence;
int wordCount = 0;
char prevChar = ' ';
char currentChar;
int index;
System.out.println("Enter a sentence: ");
sentence = EasyIn.getString();
for (index = 0; index < sentence.length(); index++)
{
currentChar = sentence.charAt(index);
if (Character.isLetter(currentChar) && !Character.isLetter(prevChar))
{
wordCount++;
}
prevChar = currentChar;
}
System.out.println("The number of words is " + wordCount + ".");
}