我有一個字符數組填充了一些字符。假設我的char數組中有「HelloWorld」。 (不是字符串,佔用0到9的索引)在char數組中插入字符
我想要做的是在數組中間插入一個字符,並將其餘的字符推到一邊以便爲新字符騰出空間被插入。
所以,我可以讓char數組擁有「Hello.World」。
char ch[15]; // assume it has "HelloWorld" in it
for(int i=0; i<=strlen(ch)-1; i++) {
if(ch[i]=='o' && ch[i+1]=='W') {
for(int j=strlen(ch)-1; j>=i+2; j--) {
ch[j] = ch[j-1]; // pushing process?
}
ch[i+1] = '.';
break;
}
}
這項工作?會有更簡單的方法嗎?我可能會在這方面考慮太複雜。
「對不起,但我應該再次質疑這個話題」。閱讀[寫出完美的問題](http://tinyurl.com/so-hints),這將幫助你得到你需要的答案。 – Johnsyweb 2012-02-14 23:11:34
相關:http://stackoverflow.com/questions/9284916/how-to-push-char-array – amit 2012-02-14 23:11:36
@Johnsyweb:現在似乎「不同」的問題對我來說。他尋求幫助,取得了一些進展,並再次陷入困境,並詢問如何解決問題。 – amit 2012-02-14 23:12:40