C++面向对象编程题 第18题

18.将字符串中指定的单词(字母子串)进行单数变复数的处理。单词之间用非字母分隔。为了简化编程,

将单词的单数变复数的规则是:直接在单词后加s。例如,单词student 的复数为students。若有下列字符串:We are student,you are student,too.
指定单词student,将字符串中所有的student修改为students。处理后的字符串为:We are students,you are students,too.
试建立一个类Modistr,完成以上工作。 具体要求如下:

  1. 私有数据成员:char *str;存放指定的字符串。

  2. 公有成员函数:

  • Modistr(char *s);构造函数,动态申请字符串空间,用s指向的串初始化str。
  • void print();输出字符串str。
  • void modify(char *wordp);将 wordp所指向的单词转换为复数。
  • ~Modistr();析构函数。
  1. 在主函数中完成对该类的测试。定义类Modistr的对象mys,用字符串” We are student,you are student,too.” 初始化mys,调用成员函数完成操作。
#include <iostream>
#include <cstring>
using namespace std;
class Modistr
{
private:
    char *str;
public:
    Modistr(const char *s){
        str = new char[strlen(s) + 1];
        strcpy(str, s);
    }
    /*
    //方法一(太蠢)
    void modify(char *word)
    {
        char temp[100] = {'\0'}, p[20]; //这里要把temp的值都设为'\0',不然temp的值会随机分配,影响strlen的值
        strcpy(temp, str);
        strcpy(p, word);
        int i = 0;
        while (i <= strlen(temp) - strlen(p))
        { //在bf算法的基础上进行修改
            int j = 0;
            while (temp[i] == p[j])
            {
                i++;
                j++;
            }
            if (j == strlen(p))
            {
                for (int k = strlen(temp); k >= i + 1; k--)
                    temp[k] = temp[k - 1];
                temp[i] = 's';
                i++;
            }
            else
                i = i - j + 1;
        }
        delete[] str;
        str = new char[strlen(temp) + 1];
        strcpy(str, temp);
    }
    */
    //方法二
    /*
    void modify(char *wordp)
    {
        int len = strlen(wordp);
        char temp[100] = {'\0'};
        strcpy(temp, str);
        for (int i = 0; i < strlen(temp); i++)
        {
            int j, count;
            for (j = 0; temp[i + j] == wordp[j] && j < len; j++);//内容空
            if (j == len)
            {
                for (int k = strlen(temp); k >= i + j + 1; k--)
                {
                    temp[k] = temp[k - 1];
                }
                temp[i + j] = 's';
                i += j;
            }
        }
        // delete[]str;
        str = new char[strlen(temp) + 1];
        strcpy(str, temp);
    }
    */
    //12.13
    void modify(const char *wordp){
        char t[100];
        strcpy(t,str);
        for(int i=0;i<strlen(t);i++){
            int j;
            for(j=0;t[i]==wordp[j];j++,i++);
            if(j==strlen(wordp)){
                t[strlen(t)+1]='\0';
                for(int k=strlen(t);k>i;k--){
                    t[k]=t[k-1];
                }
                t[i]='s';
            }
        }
        str=new char[strlen(t)+1];
        strcpy(str,t);
    }
    void print(){
        cout<<str<<endl;
    }
    ~Modistr(){
        if (str)delete[]str;   
    }
};
int main()
{
    Modistr str("We are student,you are student,too.");
    str.print();
    str.modify("student");
    str.print();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值