In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..
import java.util.Scanner;
public class Javaexcercise {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the string: ");
String str1 = in.nextLine();
System.out.print("Number of words in the string: " + countWords(str1)+"\n");
}
public static int countWords(String str1)
{
int count = 0;
if (!(" ".equals(str1.substring(0, 1))) || !(" ".equals(str1.substring(str1.length() - 1))))
{
for (int i = 0; i < str1.length(); i++)
{
if (str1.charAt(i) == ' ')
{
count++;
}
}
count = count + 1;
}
return count; // returns 0 if string starts or ends with space " ".
}
}
Result:
Enter the string: Tech Study - The complete debuggin solution
In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..
Result:
Enter the string: Tech Study - The complete debuggin solution
Number of words in the string: 7
need an explanation for this answer? contact us directly to get an explanation for this answer