How to use substring function in c?

I have a row, and I want its auxiliary row from 5th place to be the last. What function should I use?

+3
source share
4 answers

You can use the function memcpy()that is in the header file string.h.

memcpy()copies bytes of data between blocks of memory, sometimes called buffers. This function does not care about the type of data being copied - it just makes an exact copy of the byte for the byte. Function prototype

void *memcpy(void *dest, void *src, size_t count);

The dest and src arguments point to the destination and memory source blocks, respectively. count indicates the number of bytes to be copied. The return value is dest.

, - src . memmove() . memcpy() .

: http://www.java-samples.com/showtutorial.php?tutorialid=591

+4

- , &s[4] . ,

char new_str[STR_SIZE + 1] = {0};
strncpy(new_str, &s[4], STR_SIZE);
+4

, , strstr. . , , strcpy strncpy, , .

+1

If I understand correctly, you need to use some kind of delimiter to split the string in substrings. For example, "one # two # three" crashed in two three. If yes:

#include <stdio.h>
#include <string.h>
int main()
{
    char test[] = "one#two#three";
    char* res;
    res = strtok(test, "#");
    while(res) {
        printf("%s\n", res);
        res = strtok(NULL, "#");
    }

    return 0;
}

You call strtok () once with the line you want tokenize. Each of the following calls must pass NULL to continue the line from the first call. Also note that strtok can change the original pointer, so if it is dynamically allocated, you must save it before passing it to strtok.

0
source

All Articles