2014-10-11 105 views
-1

我正在写一个名为'祖马'的程序。该程序是这样工作的。如何在C/C++中将字符串修改为char数组?

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; 
} 

这是我的代码以字符数组:

#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个相同的字符像什么字符串::擦除做?

感谢您的帮助!

+0

澄清:你想用char数组来处理你已经可以用更清晰,更安全和更简洁的方式对字符串做的事情吗?顺便说一句,如果'k'达到'length() - 2',你的字符串解决方案很可能会崩溃,因为if子句将访问超出字符串长度的字符。 – Kolja 2014-10-11 13:11:51

+0

@Kolja是的,我想用char数组来做,因为我的朋友告诉我scanf比cin更快,因为scanf有字符串问题(corrent me,如果我错了)。 – warofglory 2014-10-11 13:45:28

+1

如果您使用C++,我不认为这是使用C函数的好理由。如果确实存在速度差异(这是可能的),如果您只处理几个字符(用户输入,这会引起更高的延迟超出您在CPU周期中测量的值),那么它真的很重要吗?因为,等待用户)?去'std :: cin'。如果你真的需要的话,你甚至可以在晚些时候交换它,并且大部分时间仍然可以使用字符串。如果你仍然想使用'scanf',请参阅Wimmel的答案,它就是你想要的。 – Kolja 2014-10-11 13:49:55

回答

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; 
} 
+1

谢谢,这是工作! – warofglory 2014-10-11 13:52:34

相关问题