Re: Count words of a string
Code:
int numberOfWords = 1;
string st(pBuffer);
for(int i = 1;i <= st.length(); i++)
{
// Newline
if((pBuffer[i] == '\n') && (pBuffer[i-1] != '\n'))
{
numberOfWords ++;
}
}
Something like that anyway, I haven't tried it.
Re: Count words of a string
I've followed quite same way to do this. Here is my code.
C++ Code:
bool isLastCharBlank = true;
int iWordCount = 0;
char * szTemp = szInputString;
while(*szTemp)
{
// Whitespase, tab, newline and carriage return
if ((*szTemp == ' ') || (*szTemp == '\n') || (*szTemp == '\r'))
{
isLastCharBlank = true;
}
else if (isLastCharBlank)
{
iWordCount++;
isLastCharBlank = false;
}
szTemp++;
}
cout << iWordCount;
What you think of it.