2014-10-11 87 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

相關問題