C++统计字符串中(文件中)单词个数

来源:互联网 发布:台湾手机网络制式 编辑:程序博客网 时间:2024/05/19 12:16

#include <iostream>
using namespace std;
int countWords(char* p)
{
   int iCountWordNum = 0;
   bool bIsWord = false;

   while(*p)
   {
    char c = *p;

    if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c == '\''))
    {
           bIsWord = true;
    }
    else
    {
     if (bIsWord)
     {
      iCountWordNum++;
      bIsWord = false;
     }
    }

    p++;
   }

   return iCountWordNum;
}
int main()
{
 char * ptr = "@#$*(,./Today is a good day,do you're loving it?";
    cout<<"统计出单词的总数为:"<<countWords(ptr)<<endl;
    int testf = false;
 int testt = true;
 cout<<testf<<endl<<testt<<endl;
 return 0;
}