如何在C / C ++中修改字符串到char数组?

时间:2014-10-11 07:35:21

标签: c++

我正在写一个名为'Zuma'的程序。该计划的工作原理如下。

Input:
    ACCBA // a string make up of char from 'A' to 'Z'
    5     // number of inputs
    1 B   // insert char 'B' to position '1' of the string
    0 A   // and so on...
    2 B
    4 C
    0 A

当3个相同的字符彼此相邻时,我们从字符串中删除/删除/删除它们。

例如,当我们将字符'C'插入字符串'ABCC'的第2位时,我们得到'AB'因为 'CCC'将从字符串中删除。

Output:
    ABCCBA
    AABCCBA
    AABBCCBA  // the process is AABBCCCBA -> AABBBA -> AAA -> -
    -         // if the string is empty, we output "-"
    A

这是我的字符串代码:

#include <iostream>

using namespace std;

int main()
{
    int n, pos;
    int k = 0;
    int length = 0;

    string zuma, marble; // i use string

    cin >> zuma;
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        cin >> pos >> marble;
        zuma.insert(pos, marble);

        length = zuma.length();       // length of current string

        // compare each char from pos[i] with pos[i+1] and pos[i+2]
        // and then ++i until end of string
        while (k != length && length >= 3)
        {
            if (zuma[k] == zuma[k + 1] && zuma[k] == zuma[k + 2])
            {
                zuma.erase(k, 3);     // erase 3 same char in the string
                k = 0;                // set k to zero to start from pos[0] again
            }
            else
                k++;
        }

        // if string is not empty
        if (!zuma.empty())
        {
            cout << zuma << endl;     // output the current char in the string
            k = 0;
        }
        else
            cout << "-" << endl;
    }

    return 0;
}

这是我的char数组代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

void append (char subject[], const char insert[], int pos) {
    char buf[100] = {}; 
    strncpy(buf, subject, pos); 
    int len = strlen(buf);
    strcpy(buf+len, insert);
    len += strlen(insert);

    strcpy(buf+len, subject+pos);

    strcpy(subject, buf);

}

int main()
{
    int n, pos;
    int k = 0;
    int length = 0;

    char zuma[100], marble[100];

    scanf("%s", zuma);
    scanf("%d", &n);

    for (int i = 0; i < n; ++i)
    {
        scanf("%d %s", &pos, marble);

        append(zuma, marble, pos); // acts like string::insert

        length = strlen(zuma);

        while (k != length && length >= 3)
        {
            if (zuma[k] == zuma[k + 1] && zuma[k] == zuma[k + 2])
            {
                //zuma.erase(k, 3);     // need help with this part to remove 3 same chars like string::erase
                k = 0;
            }
            else
                k++;
        }

        if (strlen(zuma) != 0)
        {
            printf("%s\n", zuma);
            k = 0;
        }
        else
            printf("%s\n","-");

     }

    return 0;
}

我的问题是如何编写一个函数来删除3个相同的字符,就像string :: erase一样?

感谢您的帮助!!

1 个答案:

答案 0 :(得分:1)

您可以使用memmove将字符串的其余部分复制到要删除的字符的位置。使用strlen确定要移动的字节数。请注意,您无法使用strcpy,因为源缓冲区和目标缓冲区重叠。

if (zuma[k] == zuma[k + 1] && zuma[k] == zuma[k + 2])
{
    int len = strlen(zuma+k+3) + 1; // +1 to copy '\0' too
    memmove(zuma+k, zuma+k+3, len);
    k = 0;
}