作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有 char *p 并且 p 有短语“这并不容易”。并且最后以 null 终止。我想搜索 p ,无论何时遇到 's',我都想用“101”代替,而不删除任何其他字符。因此,对于这个例子,p 源自“这并不容易”。到“thi101 i101 不是 ea101y”。我也只能使用 header stdio.h、stdlib.h 和 string.h。
char q[] = "s";
char r[] = "101";
for(int j = 0; s[j]!='\0'; j++){
if(s[j]==q[0]){
int temp = 0;
s[j] = r[temp];
temp++;
int temp2 = j;
while(temp<3){
for(int k = length; k>j+1; k--){
s[k]=s[k-1];
}
s[j] = r[temp];
temp++;
j++;
}
j = temp2;
}
}
}
s 是我之前提到的字符串“this is not easy.”。
我当前得到的输出是“thi011 i011 not �ea”,这是不正确的。
最佳答案
首先确定“s”(1)和“101”(3)的长度。然后,将每次出现“s”后的所有字符移动3-1=2,并放入“101”。
当然,您必须确保字符串缓冲区足够大以供操作使用。
关于c - 如何将一串字符插入到 char* 的中间并将所有字符向右移动而不删除任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19610174/
我是一名优秀的程序员,十分优秀!