gpt4 book ai didi

c - 将每个以特殊单词开头和结尾的字符串存储到 C 中的数组中

转载 作者:行者123 更新时间:2023-11-30 15:11:04 27 4
gpt4 key购买 nike

我有一个很长的字符串,我想将以特殊单词开头和结尾的每个字符串存储到一个数组中,然后删除重复的字符串。在我的长字符串中,单词之间没有空格、 或任何其他分隔符,因此我无法使用 strtok。开始标记为 start,结束标记为 end。这是我到目前为止的代码(但它不起作用,因为它使用 strtok())。

char buf[] = "start-12-3.endstart-12-4.endstart-13-3.endstart-12-4.end";
char *array[5];
char *x;
int i = 0, j = 0;
array[i] = strtok(buf, "start");

while (array[i] != NULL) {
array[++i] = strtok(NULL, "start");
}
//removeDuplicate(array[i]);
for (i = 0; i < 5; i++)
for (j = 0; j < 5; j++)
if (strcmp(array[i], array[j]) == 0)
x[i++] = array[i];

printf("%s", x[i]);

输入示例:

start-12-3.endstart-12-4.endstart-13-3.endstart-12-4.end

输出相当于:

char *array[]= { "start-12-3.end", "start-12-4.end", "start-13-3.end" };

第二个 start-12-4.end 字符串已在输出中被删除。

*我也使用过 strstr 但有一些问题:

int main(int argc, char **argv)
{
char string[] = "This-one.testthis-two.testthis-three.testthis-two.test";
int counter = 0;

while (counter < 4)
{
char *result1 = strstr(string, "this");
int start = result1 - string;
char *result = strstr(string, "test");
int end = result - string;
end += 4;
printf("\n%s\n", result);
memmove(result, result1, end += 4);
counter++;
}
}

To put string into array and remove duplicate string, I've tried following code but it has issue:

int main(void)
{
char string[] = "this-one.testthis-two.testthis-three.testthis-two.test";
int counter = 0;
const char *b_token = "this";
const char *e_token = "test";
int e_len = strlen(e_token);
char *buffer = string;
char *b_mark;
char *e_mark;
char *a[50];
int i=0, j;
char *s;

while ((b_mark = strstr(buffer, b_token)) != 0 && (e_mark =strstr(b_mark, e_token)) != 0)
{
int length = e_mark + e_len - b_mark;

s = (char *) malloc(length);

strncpy(s, b_mark, length);

a[i]=s;
i++;
buffer = e_mark + e_len;
}
for (i=0; i<strlen(s); i++)
printf ("%s",a[i]);
free(s);
/*
//remove duplicate string

for (i=0; i<4; i++)
for (j=0; j<4; j++)
{

if (a[i] == NULL || a[j] == NULL || i == j)
continue;

if (strcmp (a[i], a[j]) == 0) {
free(a[i]);
a[i] = NULL;
}
printf("%s\n", a[i]);
*/

return 0;
}

最佳答案

与您提供的示例一起使用,并在 Valgrind 中测试内存泄漏,但可能需要进一步测试。

#include <malloc.h>
#include <stdio.h>
#include <string.h>

unsigned tokens_find_amount( char const* const string, char const* const delim )
{
unsigned counter = 0;
char const* pos = string;
while( pos != NULL )
{
if( ( pos = strstr( pos, delim ) ) != NULL )
{
pos++;
counter++;
}
}

return counter;
}

void tokens_remove_duplicate( char** const tokens, unsigned tokens_num )
{
for( unsigned i = 0; i < tokens_num; i++ )
{
for( unsigned j = 0; j < tokens_num; j++ )
{
if( tokens[i] == NULL || tokens[j] == NULL || i == j )
continue;

if( strcmp( tokens[i], tokens[j] ) == 0 )
{
free( tokens[i] );
tokens[i] = NULL;
}
}
}
}

void tokens_split( char const* const string, char const* const delim, char** tokens )
{
unsigned counter = 0;
char const* pos, *lastpos;
lastpos = string;
pos = string + 1;

while( pos != NULL )
{
if( ( pos = strstr( pos, delim ) ) != NULL )
{
*(tokens++) = strndup( lastpos, (unsigned long )( pos - lastpos ));
lastpos = pos;
pos++;
counter++;
continue;
}

*(tokens++) = strdup( lastpos );
}
}

void tokens_free( char** tokens, unsigned tokens_number )
{
for( unsigned i = 0; i < tokens_number; ++i )
{
free( tokens[ i ] );
}
}

void tokens_print( char** tokens, unsigned tokens_number )
{
for( unsigned i = 0; i < tokens_number; ++i )
{
if( tokens[i] == NULL )
continue;
printf( "%s ", tokens[i] );
}
}

int main(void)
{
char const* buf = "start-12-3.endstart-12-4.endstart-13-3.endstart-12-4.end";
char const* const delim = "start";

unsigned tokens_number = tokens_find_amount( buf, delim );
char** tokens = malloc( tokens_number * sizeof( char* ) );
tokens_split( buf, delim, tokens );

tokens_remove_duplicate( tokens, tokens_number );
tokens_print( tokens, tokens_number );

tokens_free( tokens, tokens_number );
free( tokens );

return 0;
}

关于c - 将每个以特殊单词开头和结尾的字符串存储到 C 中的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35820053/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com