[英]Delete chars from string C
So i start learning C from a book, and one of the exercises was to create a function that will take 2 string and delete from the first string the characters in the second string. 因此,我从一本书开始学习C,其中一个练习是创建一个函数,该函数将使用2个字符串,并从第一个字符串中删除第二个字符串中的字符。
We stile didn't learn about pointer, so i guess this is possible without them, but when i try to run my code its crush. 我们隔断板框没有学习指针,所以我想没有它们也是有可能的,但是当我尝试运行我的代码时,它就暗恋了。
The code: 编码:
#include <stdio.h>
#include <string.h>
char squis(char string[], char sub[])
{
int i, c;
char ret_string[strlen(string)];
int map[strlen(string)];
for(i=0; i<= strlen(string);i++)
map[i] = -1;
for(i=0; i<= strlen(sub);i++)
{
while(string[c]!='\0')
{
if (string[c]==sub[i])
map[c] = c;
c++;
}
c=0;
}
for(i=0; i<= strlen(string);i++)
{
if (map[i]==-1)
ret_string[c++] = string[i];
}
ret_string[c] ='\0';
return ret_string[0];
}
int main()
{
char string[] = "string";
char remove[] = "sasas";
printf("%s",squis(string,remove));
return 0;
}
I stile newbie in C, so I think the problem lay on my lack of understanding in the way that C work. 我对C的新手不满意,所以我认为问题出在我对C的工作方式缺乏了解。
Thanks a lot for help :-) 非常感谢您的帮助:-)
Update: its seem the problem laying in the return in the end of the function. 更新:看来问题在于函数结尾处的返回。 The function seems to work well when when i print
ret_string
inside the function(except one bug that make the function ignore the first char in the sub string, but i will deal with it later), but when i try to return the array to print it in the main function its fail. 当我在函数内部打印
ret_string
时,该函数似乎运行良好(一个错误使该函数忽略了子字符串中的第一个字符,但我稍后会处理),但是当我尝试返回要打印的数组时它在主要功能上失败了。
There is specific rules for returning array in C? 有在C中返回数组的特定规则吗?
Here is an obvious problem: 这是一个明显的问题:
int map[strlen(string)];
for(i=0; i<= strlen(string);i++)
map[i] = -1;
You create an array of strlen(string)
characters, and then you initialize strlen(string) + 1
characters in the array. 您创建一个由
strlen(string)
字符组成的数组,然后在数组中初始化strlen(string) + 1
字符。 Writing out of bounds of an array leads to undefined behavior , where anything could happen. 超出数组的范围写入会导致未定义的行为 ,这可能会发生任何事情。
Change the loop condition to less-than <
. 将循环条件更改为小于
<
。 You should probably do it in all your loops. 您可能应该在所有循环中都这样做。
You have a similar problem with ret_string
, which will be the string you return. ret_string
也有类似的问题,这将是您返回的字符串。 It's going to be at most strlen(string)
characters, but then you need to add one character for the string terminator so the array needs to be strlen(string) + 1
long. 最多只能有
strlen(string)
字符,但是随后您需要为字符串终止符添加一个字符,因此数组需要为strlen(string) + 1
长strlen(string) + 1
。
Then you have the problem that the squis
function only return a single character but in your printf
call you treat this single character as a string. 然后,您会遇到一个问题,即
squis
函数仅返回单个字符,但是在您的printf
调用中,您将此单个字符视为字符串。 This should make your compiler scream a warning at you. 这应该使您的编译器向您发出警告。 If you fix this by simply returning
ret_string
you will have another case of undefined behavior , as you then return a pointer to a local variable, which goes out of scope when the function exits, so the returned pointer is no longer valid. 如果仅通过返回
ret_string
解决此问题,则将出现另一种不确定行为的情况 ,因为您随后将返回一个指向局部变量的指针,该局部变量在函数退出时会超出范围,因此返回的指针不再有效。 And if you decide to allocate ret_string
on the heap, with the current call you have a memory leak as then the pointer is not saved so you can free the allocated memory. 而且,如果您决定在堆上分配
ret_string
,则在当前调用中会发生内存泄漏,因为指针不会保存,因此您可以释放分配的内存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.