class CWord
{
public:
CWord(char *s);
void process();
int issub(char *s);
CWord operator-(CWord& s);
void print();
private:
char str[80];//存放字符串
int count;//英文单词的个数
};
CWord::CWord(char *s)
{
strcpy_s(str, "");
if(s != NULL)
{
strcpy_s(str, s);
}
count = 0;
}
void CWord::print()
{
cout << "字符串为:"<< str;
cout << ",单词个数为:" << count <
void CWord::process()
{
char* temp = str;
temp++;
while(*temp != '\0')
{
if(*temp == 32 && *(--temp) != 32)
{
count++;
}
temp+=2;
}
if(*(--temp) != 32)
{
count++;
}
}
int CWord::issub(char* sub_str)
{
//在字符串str中查找s串,若找到,返回指定串的首字符的位置,若没有,则返回-1
int pos = 0;
char* src_str = str;
while (*src_str != '\0')
{
pos++;
if(*src_str == *sub_str)
{
char* temp1 = src_str;
char* temp2 = sub_str;
while(*temp2 != '\0' && *temp1 != '\0')
{
if(*(temp1++) != *(temp2++))
break;
}
if(*temp2 == '\0')
{
return pos;
}
}
src_str++;
}
return -1;
}