c++编程,求大神!建立一个WORD类,统计一个英文字符串中的英文单词个数及在字符串中查找指定子串。

2020-06-22 科技 125阅读
时间有限,粗略写了几个,看看吧,写的不是那么完全就是了。
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;
}
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com