为什么我的删除功能中存在错误
why there is an error in my removestring function
我正在制作一个称为 removeString()
的函数。目的是从文本中删除字符串
示例:如果文本是"错误的儿子",我想删除"错误"为"儿子",我使用此功能。
原型是:
void removeString(char source[], int start, int numofchrem);
和这样的程序就是这样:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void removeString(char source[], int start, int numofchrem)
{
int i, j, k;
char n[20];
j = start + numofchrem - 1;
for (i = 0; i < numofchrem; i++)
{
if (source[start] == ' ')
break;
source[j] = 'b';
j--;
}
printf("%s", source);
}
int main()
{
char text[] = "the wrong son";
void removeString(char source[], int start, int numofchrem);
removeString(text, 4, 6);
}
当我制作程序时,我首先调试源中的字符,就像这个
"the bbbbbbson"
当我使用以下方式打印文本时:
printf("%s",source);
该程序仅显示"儿子"而不是"儿子"。因此,如果有人能帮助我,我会非常感激。
您需要将"son"
(和' '
(移回,以替换的内容
void removeString(char source[], int start, int numofchrem)
{
char *d = source + start; // d points to "wrong..."
char *s = source + start + numofchrem; // s points to "son"
while (*s != ' ') {
*d = *s; // replace 'w' with 's', 'r' with 'o', ...
d++; // point to next: "rong...", "ong...", "ng..."
s++; // point to next: "on", "n"
}
*d = ' '; // terminate string
}
相关文章:
- 我正在尝试制作一个自平衡机器人,但编译时存在错误。我不知道如何解决它
- Visual Studio 2019在内置库中存在错误
- 以下 OpenCV 源代码中是否存在错误?(#ifdef 没有 #else)
- 在存在错误代码的情况下输出参数与 NRVO
- 为什么我的删除功能中存在错误
- 哪个编译器(如果有的话)在参数包扩展中存在错误
- 未知功能已经存在错误
- progam在std :: getline AM之后停止响应,代码中是否存在错误
- Netuseradd用户已经存在错误
- GCC 4.4.1是否存在错误,因为它不接受ctor初始值设定项中的注入类名
- Visual C++中的外部模板是否存在错误
- 类模板成员不存在错误
- 如果构造函数中存在错误,请停止进程
- purify中存在错误的内存泄漏
- C++中是否存在错误类型
- 来自 vtable 的未定义符号是否意味着接口和实现之间存在错误
- 为什么我遇到此代码的分段错误?此代码几乎没有运行,因此构造函数或复制构造函数中可能存在错误
- c ++ 11 参数包在 Apple LLVM 7.0.0 中存在错误行为,但适用于 GCC-5.1
- 文件输出中存在错误字符
- 在boost::mutex构造函数中同时写入同一内存区域可能存在错误