简体   繁体   中英

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). Our only hint is that its something to do with '\\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.

C strings are "null terminated" which means they are considered to end wherever a null (written '\\0' in C) appears.

If I start with the string "Stack Overflow" and I overwrite the space with '\\0' , I now have the string "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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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