Tuesday, December 15, 2009

Count words in a sentence

public int countWords(string s1)
{
int cnt = 0;
//char space = ' ';
int i = 0;
for (i = 0; i < s1.Length; i++)
{
if (s1[i] == ' '){}
else if (s1[i] != ' ' && i == 0) cnt++;
else if (s1[i] != ' ' && s1[i - 1] != ' ') { }
else if (s1[i] != ' ' && s1[i - 1] == ' ') cnt++;
}
return cnt;
}

No comments:

Node.JS rest api Tutorials