简体   繁体   中英

is there already a function similar to this in any C lib?

I've been stalking this place for a while but this is my first post so please be indulgent. I've coded this function for a school project, but don't know how to name it (we have to make our own functions). Question is, does anyone know if there's a similar function in a library, just so I know how to name it ?

void function(char *str, int slen)
{
    while(*str) {
        if (slen) {
            *str = *(str + slen);
        } else {
            *str = '\0';
        }

        if (*(str + slen) == '\0') {
            slen = 0;
        }

        str++;
    }
}

What it does:

Slide an string by SLEN chars towards the beginning, filling the end with '\\0' . Example :

char *str = strdup("I want to remove 8 characters");
function(str, 8);

turns my string into "remove 8 characters.\\0\\0\\0\\0\\0\\0\\0\\0\\0"

You can use memcpy (or memmove if the from and to areas overlap)

and then memset

There is no one function that does both

If you want to pass it to a function that wouldn't be responsible for cleaning the memory later you can just give that function str+len :

printFunc(str+len);

but if you need to pass this string on without the first len chars so you can memcpy the string onto a new string and free the last one.

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