diff --git a/.DS_Store b/.DS_Store index 1a232cde1..dd05b9be5 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/countwords.java b/countwords.java new file mode 100644 index 000000000..78f6eb2bd --- /dev/null +++ b/countwords.java @@ -0,0 +1,23 @@ +class CountTheWords +{ + 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++) + { + if((s.charAt(i) == ' ') && (s.charAt(i+1) != ' ')) + { + count++; + } + } + + System.out.println("Number of words in a string = "+count); + } +} \ No newline at end of file