Character array parsing

Let's say I have "game.abc" as a string (an array of characters) and I want to get the characters that are before the dot. What do you think is the most efficient way to do this in C?

Is there a "while while. Copy char to char" way to go or is there another way?

I hope this question can help others as well.

Thank!

+3
source share
4 answers

you can also use strchr()and then strncpy()to find the first instance of '.' in your line and then copy all the characters in front of it to another line

+3
source

, . /, binW .

, ( ) , , .

, .

+1

, , . , , , .

:

char *in_ptr = in_string;
char *out_ptr = out_buffer;

while(*in_ptr != '.')
{
    *out_ptr = *in_ptr;
    in_ptr++;
    out_ptr++;
}
*out_ptr = '\0';

.

, , :

char *in_ptr = in_string;

while(*in_ptr != '.') in_ptr++;

out_string = (char *)malloc(in_ptr - in_string + 1);
memcpy(out_string, in_string, (in_ptr - in_string));
out_string[in_ptr - in_string] = '\0';

, . . , , , (in_ptr - in_string) , . strchr(), memcpy(). memcpy, "." , .

( , ), , , . .

+1

strtok() tokenise.

#include <stdio.h>
main()
{

char str[]="games.abc";
char *tmp,*tmp1;

tmp = strtok(str,".");

printf("tmp %s\n",tmp);

tmp1 = strtok(NULL,"\0");
printf("tmp1 %s\n",tmp1);

}

./tmp1
tmp games
tmp1 abc
0

Source: https://habr.com/ru/post/1778100/


All Articles