diff --git a/countwords.java b/countwords.java index 78f6eb2bd..a1d4d72db 100644 --- a/countwords.java +++ b/countwords.java @@ -1,23 +1,19 @@ +import java.util.Scanner; class CountTheWords { - public static void main(String[] args) + public static void main(String args[]) { System.out.println("Enter the string"); - Scanner sc = new Scanner(System.in); - String s=sc.nextLine(); - - int count = 1; - - for (int i = 0; i < s.length()-1; i++) + int count = 1; + for (int i = 0; i < s.length()-1; i++) { if((s.charAt(i) == ' ') && (s.charAt(i+1) != ' ')) { - count++; + count++; } } - - System.out.println("Number of words in a string = "+count); + System.out.println("Number of words in the string = "+count); } } \ No newline at end of file