import java.util.Scanner; public class WordCount { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter your string:"); String str = in.next();...


import java.util.Scanner;
public class WordCount
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter your string:");
String str = in.next();
countWords(str);
System.out.print(countWords(str));


}


public static int countWords(String str)
{
int count = 0;
for (int i = 0; i < str.length()-1;="">
{
if ((str.charAt(i)==' ')&&(!(str.charAt(i+1)==' ')))
{
count++;
}
}


return count;
}
}


This is my code for counting the number of words in a string. This program compiles, but the answer is always 0. I don't know why the program is not considering the increment of count. I would appreciate help in this. Thanks!



Jun 08, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here