Count words in an sentences



Problem to count words in an string.



Example-1:


Input    : S = "mycrazycoding is a programming website"
Output   : 5  


Example-2:


Input    : S = "hello world"
Output   : 2  







Solution




public class Main
{
    public static void main(String [] args)
    {
        String s = "mycrazycoding is a programming website";

        String str [] = s.split(" ");

        int count = 0;

        for (int i = 0; i<str.length; i++) 
        {
            if(str[i].isEmpty())
            {
                continue;
            }

            count++;
        }

        System.out.print(count);
    }
}
n = "mycrazycoding is a programming website"

m = n.split()

print(len(m))



Output



5