2014-03-25 47 views
-1

這是我第一次問,所以請幫忙。我的問題是如何添加任何字符之間的任何字符之間,意味着加入後每c(小寫字母),但我不想使用任何功能,我想寫我自己的無效函數與傳遞只有一個參數應該是char數組,可以幫忙嗎?C++在字符串數組中添加任何字符

我學會了如何檢查每一個字符的字符串與

while(*p!='\0') 
{ 

/// What should I write here to check if there is any dot , then add after it 
/// a small c 

p++; 
} 
+0

您檢查它是否不爲空。這個過程對於任何你想比較的其他角色來說都是一樣的。 – shawn1874

+0

你真的想用C數組的char而不是C++ std :: string嗎? C數組更加棘手,您還需要將數組中可用的內存長度傳遞給函數。您是否必須在原地進行操作,或者輸出字符串是否可以位於不同的陣列中?後者會更容易。 – sj0h

+0

謝謝,但我檢查它是否等於'。'但我無法解決更多的問題,另一件事我想如何插入內部公羊的角色,我的意思是與任何數量的字符陣列,可以消耗它? – user3457718

回答

0

如果你要做到這一點的C方式,我建議你嘗試這樣的事:

void adjust_string(char*output_p, int output_space, const char* input_p) 
{ 
    //while (there is still input left, and room in output buffer) { 
    while (*input_p!='\0' && output_space>2) { 
     //copy input character to output 

     //update the output pointer 

     //update the amount of room left in the output buffer 

     //if (its a special character) { 

      //add the extra character to output 

      //update the output pointer 

      //update the amount of room left in the output buffer 

     } 
     //update the input pointer 
     input_p++; 

    } 
    //null-terminate the output string 

} 

在調用這個函數的函數中,你需要提供一個數組來放置輸出並指定它的長度,所以你不能得到緩衝區溢出。

注意:在檢查輸出緩衝區中的空間時,您需要考慮添加額外字符的可能性以及終止空字符的空間。

+0

謝謝,但像這樣更困惑,我的意思是我可以傳遞一個參數,這是陣列的基地址。我如何在ram中更改occpied字節(當我添加更多字符時)? – user3457718

+0

你真的需要那樣做嗎?我問的原因是因爲它使得它更加複雜 – sj0h

+0

您不能只傳入基本數組地址,然後繼續增加該數組的大小。最好還可以傳遞分配的內存長度,並使用它來增加該數組中字符串的長度。但是,你不能用const char數組來做這件事,就像用字符串文字一樣。 – sj0h