Count Words

 

For a given input string(str), find and return the total number of words present in it.

Sample Input 1:
this is a sample string
Sample Output 1:
5
public class Solution {

	public static int countWords(String str) {	
		//Your code goes here
        
        int l=str.length();
        int c=0;
        str=" "+str;
        for(int i=0;i<l;i++)
            
        {
        char ch=str.charAt(i);
            
			
        	if(ch==' ')
            {
                c++;
            }
        }
        return c;
	}

}

Comments

Popular posts from this blog

Minimum Length Word

Check Number Sequence

Star Pattern