搜索数组的字符串

时间:2011-09-19 04:23:37

标签: c cstring

我将如何实现这一点?我试图将c字符串数组与单个字符串进行比较,如果没有匹配则将其附加到2d数组。

char*mprt,uni[100][16];
    mprt = &uni[0][0];
for (int s = 0;s <= 99;s++)
        {
            for (int z = 0;z <= 15;z++)
            {
                if (strcmp(mprt++, string1) != 0)
                {
                    uni[s][z] = string1[z];
                }
            }
        }

3 个答案:

答案 0 :(得分:2)

在你的for循环中,你需要复制整个字符串以附加它,

用此替换该行,

strcpy(uni[s], string1[z]);

考虑string1[z]是char指针数组的元素。

修改

不确定这是否是您要尝试的内容,但您最终会将所有元素设置为string1

char string1[] = "String";

char uni[100][16] = {};

for (int s = 0; s < 100; s++)
{
    if (strcmp(uni[s], string1) != 0)
    {
        strcpy(uni[s], string1);
    }
}

或者没有strcpy()

char string1[] = "String";

char uni[100][16] = {};

for (int s = 0; s < 100; s++)
{
    for (int r = 0; r < sizeof(string1); r++)
    {
        uni[s][r] = string1[r];
    }
}

答案 1 :(得分:2)

好的...从您的评论中我现在得到您正在尝试做的事情。你想把它变成一个函数,这样你就可以向它提供单词,但它应该让你指向正确的方向。

请注意,您可以使用char[][],但这样您的字符串可以是任意长度,因为我们在将它们放入列表时会动态分配它们。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{

    /* space for 100 strings */
    char **uni = calloc(100, sizeof(char*));
    char **i;

    /* Put one word in the list for test */
    *uni = calloc(5, sizeof(char*));
    strncpy(*uni, "this", 5);

    /* here's the string we're going to search for */
    char * str2 = "that";

    /* go through the first dimension looking for the string 
       note we have to check that we don't exceed our list size */
    for (i = uni; *i != NULL && i < uni+100; i++)
    {
        /* if we find it, break */
        if (strcmp(*i,str2) == 0)
            break;
    }

    /* if we didn't find the string, *i will be null 
     * or we will have hit the end of our first dimension */
   if (i == uni  + 100)
   {
        printf("No more space!\n");
   }        
   else if (*i == NULL)
   {
        /* allocate space for our string */
        *i = calloc(strlen(str2) + 1, sizeof(char));

        /* copy our new string into the list */
        strncpy(*i, str2, strlen(str2) + 1);
    }


    /* output to confirm it worked */
    for (i = uni; *i != NULL && i < uni+100; i++)
        printf("%s\n",*i);
}

为完整起见,char[][]版本:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{

    char uni[100][16];
    int i,j;

    /* init our arrays */
    for (i=0;i<100;i++)
        for (j=0;j<16;j++)
            uni[i][j] = '\0';


    /* Put one word in the list for test */
    strncpy(uni[0], "this",15);

    /* here's the string we're going to search for */
    char * str2 = "that";

    /* go through the first dimension looking for the string */
    for (i = 0; uni[i][0] != '\0'  && i < 100; i++)
    {
        /* if we find it, break */
        if (strcmp(uni[i],str2) == 0)
            break;
    }

    /* if we didn't find the string, uni[i][0] will be '\0'
     * or we will have hit the end of our first dimension */
    if (i == 100)
    {
        printf("No more space!\n");
    }
    else if (uni[i][0] == '\0')
    {
        /* copy our new string into the array */
        strncpy(uni[i], str2, 15);
    }

    /* output to confirm it worked */
    for (i = 0; uni[i][0] != '\0' && i < 100; i++)
        printf("%s\n",uni[i]);
}

编辑以解释以下评论中的C指针和数组:

在C中,数组降级为指针。当你第一次开始时,这确实令人困惑。

如果我有char myArray[10]并且我想将其传递给带有char *参数的函数,我可以使用&myArray[0]myArray。当你离开索引时,它会降级为指向数组中第一个元素的指针。

在像你这样的多维数组中,&uni[5][0] == uni[5] - 两者都指向第一个中索引为5的第二个维度中的第一个元素。它降级为char*指向列表中第6个单词的开头。

答案 2 :(得分:0)

要附加到2D数组的末尾,您需要使用动态内存分配

    const int row_max = 100, col_max = 16;
char** uni = NULL;
char searchString[col_max] = "xyz";
int currentLength = 0;

uni = (char**) malloc (row_max * sizeof(char*)); //TODO:error handling code to be added
for (int row = 0; row < row_max; row++)
{
    uni[row]    = (char*)malloc(col_max * sizeof(char));//TODO:error handling code to be added 
    currentLength = row;
}

for (int row = 0; row < row_max; row++) //fill array uni with data here
{
    uni[row] = "abc";
}

for (int row = 0; row < row_max; row++)
{
    for (int col = 0; col < col_max; col++)
    {
        if (strcmp(&uni[row][col], searchString) != 0 ) 
        {//string not found

            uni = (char**)realloc(uni, (currentLength + 1) * sizeof(char*));//TODO:error handling code to be added 
            uni[currentLength + 1] = (char*)malloc(col_max);//TODO:error handling code to be added 
            currentLength++;
            strcpy(uni[currentLength],searchString); //append at end of 2D array 
            goto stop;
        }
    }
}

车站:     for(int row = 0; row&lt; = currentLength; row ++)         自由(UNI [行]);     自由(UNI);

return 0;