Problem A: 字符串类(I)

字符串类(I)

题目

Problem A: 字符串类(I)

Problem A: 字符串类(I)

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 7972   Solved: 3887
[ Submit][ Status]

Description

封装一个字符串类,用于存储字符串和处理的相关功能,支持以下操作:

1. STR::STR()构造方法:创建一个空的字符串对象。

2. STR::STR(const char *)构造方法:创建一个字符串对象,串的内容由参数给出。

3. STR::length()方法:返回字符串的长度。

4. STR::putline()方法:输出串的内容,并换行。

-----------------------------------------------------------------------------

你设计一个字符串类STR,使得main()函数能够正确运行。

函数调用格式见append.cc。

append.cc中已给出main()函数。

-----------------------------------------------------------------------------

Invalid Word(禁用单词)错误:“string”、“vector”等被禁用。

Input

输入有若干行,每行一个字符串。

Output

每组测试数据对应输出一行,包含两部分内容,首先是一个整数,表示输入串的长度,然后是输入的字符串,两者用一个空格分开。格式见sample。

Sample Input

A 123456789

Sample Output

0 12 Hello World! 1 A 9 123456789

HINT

Append Code

[ Submit][ Status]

正确代码

    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    class STR
    {
    private:
        char *str;
        int len;
    public:
        STR():str(NULL),len(0){;}
        STR(const char *ch)
        {
            int i;
            for(i=0;ch[i]!='\0';i++);
            len=i;
             str = new char [len+1];
             for(i=0;i<len;i++)
                str[i] = ch[i];
            str[len]='\0';
        }
        ~STR()
        {
            if(str!=NULL)
            delete []str;
        }
        int length()
        {
            return len;
        }
        void putline()
        {
            if(len==0)
                cout<<endl;
            else
            cout<<str<<endl;
        }
    };
    int main()
    {
        STR e;
        STR h("Hello World!");
        char s[100001];
        cout << e.length() << " ";
        e.putline();
        cout << h.length() << " ";
        h.putline();
        while(gets(s) != NULL)
        {
            STR str(s);
            cout << str.length() << " ";
            str.putline();
        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值