搜索字符串数组 [英] search array for string

查看:113
本文介绍了搜索字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How would i implement this?Im trying to compare a array of c strings to a single string and if there is no match append it to the 2d array.

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


推荐答案

好吧...从您的评论中,我现在知道了您要尝试做的事情。您希望将其变成一个函数,以便可以向其输入单词,但是它应该使您指向正确的方向。

Ok ... from your comments I now get what you're trying to do. You'd want to make this into a function so you could feed words to it, but it should get you pointed in the right direction.

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

Note that you can use char[][], but this way your strings can be of any length because we dynamically allocate them when we put them in the list.

#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中,数组降级为指针。

In C, arrays degrade to pointers. This is actually really confusing when you first start.

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

If I have char myArray[10] and I want to pass that to a function that takes a char * argument, I can use either &myArray[0] or just myArray. When you leave off the index, it degrades to a pointer to the first element in the array.

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

In a multidimensional array like yours, &uni[5][0] == uni[5] - both are pointers to the first element in the second dimension at index 5 in the first. It degrades to char* pointed at the beginning of the 6th word in your list.

这篇关于搜索字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆