Problem G: 字符串类(II)

字符串类,嗯。禁用string头文件,嗯。没啥好说的。

Description

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

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

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

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

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

5. 运算符“+”和“+=”,表示两个字符串的连接运算,规则为:

   c = a + b 表示串c中的字符是a和b的连接:“a+b”的结果是一个新的字符串,串a和串b的内容不变。

   a += b    表示串a中的字符是a和b的连接:串b中的内容不变

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

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

函数调用格式见append.cc。

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

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

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

Input

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

Output

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

Sample Input

A123456789

Sample Output

12 Hello World!0 12 Hello World!12 Hello World!12 Hello World!10 A1234567891 A9 12345678910 123456789A1 A

HINT

下面附上ac代码:
#include <iostream>
#include <cstdio>                                        //支持gets输入
using namespace std;
//代替string里的strlen函数
int len(const char *a)
{
    int i;
    for(i = 0; a[i]; i++);
        return i;
}
//代替string里的strcpy函数
void cpy(char *a,const char *b)
{
    int i;
    for(i = 0; b[i]; i++)
        a[i] = b[i];
    a[i] = 0;
}

class STR
{
private:
    char *s;
    int l, siz;
public:
    STR() : l(0), siz(0), s(NULL) {  }
    STR(const char* c)
    {
        l = len(c);
        siz = 2 * l;
        s = new char[siz];
        cpy(s,c);
    }
    ~STR()
    {
        delete[] s;
    }
    int length()
    {
        return l;
    }
    void putline() const
    {
        for(int i = 0; i < l; i++)
            cout<<s[i];
        cout<<endl;
    }

    friend STR operator+(const STR&,const STR&);
    STR& operator+=(const STR &t)
    {
        int len = l + t.l;
        siz = len * 2;
        char *ss = new char[siz];
        for(int i = 0; i < l; i++)
            ss[i] = s[i];
        for(int j = l; j <= len; j++)
            ss[j] = t.s[j - l];
        delete[] s;
        s = ss;
        l = len;
        return *this;
    }
};

STR operator+(const STR& s,const STR& t)
{
    STR r;
    int i;
    r.l = s.l + t.l;
    r.siz = r.l * 2;
    r.s = new char[r.siz];
    for(i = 0; i < s.l; i++)
        r.s[i] = s.s[i];
    for(int j = i; j <= i + t.l; j++)
        r.s[j] = t.s[j - s.l];
    return r;
}

int main()
{
    STR e;
    STR h("Hello World!");
    STR he = e + h;
    cout << he.length() << " ";
    he.putline();
    cout << e.length() << " ";
    e.putline();
    cout << h.length() << " ";
    h.putline();
    e += h;
    cout << e.length() << " ";
    e.putline();
    cout << h.length() << " ";
    h.putline();

    char s1[100001], s2[100001];
    while(gets(s1) != NULL && gets(s2) != NULL)
    {
        STR str1(s1), str2(s2);
        STR str = str1 + str2;
        cout << str.length() << " ";
        str.putline();
        cout << str1.length() << " ";
        str1.putline();
        cout << str2.length() << " ";
        str2.putline();
        str2 += str1;
        cout << str2.length() << " ";
        str2.putline();
        cout << str1.length() << " ";
        str1.putline();
    }
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这道题目要求我们将字符串中每个单词的首字母变成大写。我们可以先将字符串按照空格分割成单词,然后对每个单词进行处理。处理的方法是将单词的第一个字符转换成大写,然后再将剩余的字符拼接起来。最后将处理后的单词再拼接成一个字符串即可。 ### 回答2: 题目要求编写一个程序,将给定字符串的每个单词的首字母变为大写。 首先,我们可以使用`split()`方法将字符串分割成单词的列表。然后,遍历每个单词,将首字母转化成大写。最后,使用`join()`方法将单词列表重新连接成字符串,得到结果。 以下是一个可能的实现: ```python def capitalize_first_letter(sentence): words = sentence.split() # 将字符串分割成单词列表 result = [] for word in words: capitalized_word = word[0].upper() + word[1:] # 将首字母变为大写 result.append(capitalized_word) return ' '.join(result) # 将单词列表重新连接成字符串 # 测试示例 print(capitalize_first_letter('hello world')) # 输出: Hello World print(capitalize_first_letter('zero starting point')) # 输出: Zero Starting Point ``` 以上代码中,`capitalize_first_letter`函数接收一个字符串作为参数,并返回首字母变为大写的结果字符串。我们将字符串使用`split()`方法分割成单词列表,然后遍历每个单词,将首字母转化成大写。最后,使用`join()`方法将单词列表重新连接成字符串,并返回结果。 通过上述实现,我们可以满足题目要求,将给定字符串的每个单词的首字母变为大写。 ### 回答3: 问题g:零起点学算法106——首字母变大写 这个问题要求我们将给定字符串中每个单词的首字母变为大写。解决这个问题的方法有很多种,下面我将提供两种方法。 方法一:使用内置函数capitalize() 我们可以使用内置函数capitalize()来将字符串的首字母变为大写。首先,我们将给定的字符串按照空格分割成单词列表。然后,对于每个单词,我们使用capitalize()函数将其首字母变为大写,并将其添加到结果列表中。最后,我们将结果列表连接成一个字符串,并返回这个结果字符串作为答案。 def capitalize_words(sentence): words = sentence.split() capitalized_words = [word.capitalize() for word in words] return ' '.join(capitalized_words) 方法二:逐个字符遍历 我们也可以逐个字符遍历给定字符串,当遇到空格时,将下一个字符变为大写。为了解决字符串首字母的问题,我们可以在遍历前在字符串的首部添加一个空格。 def capitalize_words(sentence): sentence = ' ' + sentence n = len(sentence) result = '' for i in range(1, n): if sentence[i - 1] == ' ': result += sentence[i].upper() else: result += sentence[i] return result 这两种方法都可以解决问题g中的要求,使用内置函数capitalize()的方法较为简单和直观,但如果想要了解更多底层的实现细节,逐个字符遍历的方法也是一种不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值