Counting the number of words in string
This commit is contained in:
parent
b0027cd585
commit
df20459b6c
@ -1,15 +1,12 @@
|
||||
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++)
|
||||
{
|
||||
if((s.charAt(i) == ' ') && (s.charAt(i+1) != ' '))
|
||||
@ -17,7 +14,6 @@ class CountTheWords
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Number of words in a string = "+count);
|
||||
System.out.println("Number of words in the string = "+count);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user