簡體   English   中英

搜索數組的字符串

[英]search array for string

我將如何實現這一點?我試圖將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];
                }
            }
        }

在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];
    }
}

好的......從你的評論我現在得到你想要做的。 你想把它變成一個函數,這樣你就可以向它提供單詞,但它應該讓你指向正確的方向。

請注意,您可以使用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[][]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個單詞的開頭。

要附加到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;
        }
    }
}

stop:for(int row = 0; row <= currentLength; row ++)free(uni [row]); 自由(UNI);

return 0;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM