简体   繁体   English

在不使用 C 中的任何库的情况下从字符数组中删除子字符串

[英]Removing a substring from a char array without using any libraries in C

I am a computer science first year student and our teachers gave a binary pattern search task to do.我是一名计算机科学一年级的学生,我们的老师给了一个二进制模式搜索任务。 We have to remove a substring from a string without using any libraries and built-ins like(memmove or strstr).我们必须在不使用任何库和内置函数(如(memmove 或 strstr))的情况下从字符串中删除子字符串。 Our only hint is that its something to do with '\\0'.我们唯一的提示是它与'\\0'有关。 I don't see how we are going to achive this because as i know the null character only ends a string not removes it.我不知道我们将如何实现这一点,因为我知道空字符只结束一个字符串而不是删除它。 And given an unknown input it gets even harder to get around.给定一个未知的输入,它变得更难绕过。 I need help about the usage of null character.我需要有关空字符使用的帮助。 EDIT: Oh and also we are not allowed to create new arrays.编辑:哦,我们也不允许创建新数组。 EDIT2: The problem is much more complicated if you are here for the solution read the comments under this thread and the marked solution's also. EDIT2:如果您在这里寻求解决方案,则问题要复杂得多,请阅读此线程下的评论以及标记的解决方案。

C strings are "null terminated" which means they are considered to end wherever a null (written '\\0' in C) appears. C 字符串是“空终止”,这意味着它们被认为在空值(在 C 中写成'\\0' )出现的任何地方结束。

If I start with the string "Stack Overflow" and I overwrite the space with '\\0' , I now have the string "Stack".如果我从字符串 "Stack Overflow" 开始并用'\\0'覆盖空格,我现在有字符串 "Stack"。 The storage for "Overflow" still exists, but it is not part of the string according to C functions like strlen() , printf() etc. In fact, if I hold a pointer to the "O" part of the original string, it will be just as if there are two strings: "Stack" and "Overflow", and you can still use both of them. “溢出”的存储仍然存在,但根据strlen()printf()等 C 函数,它不是字符串的一部分。事实上,如果我持有一个指向原始字符串的“O”部分的指针,就好像有两个字符串:“Stack”和“Overflow”,您仍然可以同时使用它们。

It's like if I come to where you live and I build a huge wall across the road just before your house.就好像我来到你住的地方,在你家门前的马路对面建了一堵巨大的墙。 The road is now shortened, and people on my side of it won't know you are there.这条路现在缩短了,我这边的人不会知道你在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM