C 搜索字符串数组
我将如何实现这一点?我试图将一个c字符串数组与一个字符串进行比较,如果没有匹配项,则将其附加到2d数组中C 搜索字符串数组,c,cstring,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
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];
}
}
}
char*mprt,uni[100][16];
mprt=&uni[0][0];
for(int s=0;s在for循环中,需要复制整个字符串以附加它
用这个替换线路
strcpy(uni[s], string1[z]);
考虑到string1[z]
是字符指针数组的一个元素
编辑:
不确定这是否是您试图执行的操作,但最终所有元素都将设置为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);
}
}
在for循环中,需要复制整个字符串以附加它
用这个替换线路
strcpy(uni[s], string1[z]);
考虑到string1[z]
是字符指针数组的一个元素
编辑:
不确定这是否是您试图执行的操作,但最终所有元素都将设置为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);
}
}
好的…从你的评论中,我现在明白了你想要做的事情。你想把它变成一个函数,这样你就可以给它输入单词,但它应该让你指向正确的方向
请注意,您可以使用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);
}
编辑以解释下面评论中的C指针和数组:
在C语言中,数组降级为指针。当您第一次启动时,这实际上是令人困惑的
如果我有char myArray[10]
并且我想把它传递给一个接受char*
参数的函数,我可以使用&myArray[0]
或者只使用myArray
。当你不使用索引时,它会退化为指向数组中第一个元素的指针
在像您这样的多维数组中,&uni[5][0]
==uni[5]
-这两个都是指向第二维度第一个索引5处的第一个元素的指针。它降级为char*
,指向列表中第六个单词的开头。好吧……从您的评论中,我现在明白了您要做的事情。您希望将其转换为一个函数,以便可以向其输入单词,但它应该会让您得到p指向正确的方向
请注意,您可以使用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);
}
编辑以解释下面评论中的C指针和数组:
在C语言中,数组降级为指针。当您第一次启动时,这实际上是令人困惑的
如果我有char myArray[10]
并且我想把它传递给一个接受char*
参数的函数,我可以使用&myArray[0]
或者只使用myArray
。当你不使用索引时,它会退化为指向数组中第一个元素的指针
在像您这样的多维数组中,&uni[5][0]
==uni[5]
-这两个都是指向第一个索引5处第二维度中第一个元素的指针。它降级为指向列表中第六个单词开头的char*
。要附加到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;
}
}
}
要附加到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;
}
}
}
当字符串相等时,strcmp的返回值为零。是的,我知道,我正在检查是否没有匹配,是否没有将字符串添加到数组中。这是家庭作业吗?如果是,那很好,但它应该有家庭作业标记你的问题与你的提问方式不太一样。你不能“追加”对于列表,它是一个固定大小。如果你的意思是数组开始时没有满,那么你必须跟踪数组中有多少单词。这就是你想要做的吗?可能我使用了错误的措辞。uni[100][16]最初为空,字符串1不是,我希望它扫描数组以查找匹配项,如果uni中没有匹配项,则将字符串添加到数组中。当字符串相等时,strcmp的返回值为零。是的,我知道,我正在检查是否没有匹配项,如果没有,则将字符串添加到数组中。这是家庭作业吗?如果是,这很好,但应该是做家庭作业
标记你问问题的方式有点不合理。你不能“附加”到列表中;列表是固定大小的。如果你的意思是数组开始时没有满,你必须跟踪其中有多少单词。这就是你想做的吗?可能我用错了词。uni[100][16]最初为空,字符串1不是,我希望它扫描数组以查找匹配项,如果uni中没有匹配项,则将字符串添加到数组中。string1声明为此字符string1[16];@Bob:复制字符串之前,需要确保字符串以null结尾“\0”。从char”到const char*”的转换无效@Bob:在char string1[16]
的情况下,string1
是指向字符串的指针,string1[16]
是一个字符数组。@Bob:没错,您不需要嵌套的for循环。string1被声明为这个字符string1[16];@Bob:在复制它之前,您需要确保字符串以null结尾'\0'。从char'到
const char*'的转换无效。@Bob:在char string1[16]的情况下
,string1
是您指向字符串的指针,string1[16]
是一个字符数组。@Bob:没错,而且您不需要嵌套的for循环。谢谢您键入所有内容,我看看我能从中得到什么,我正在添加字符[]……一秒钟,这有点高级。哦,我觉得我更容易理解fyi=strncpy()
是strcpy()的安全版本
-第三个参数限制了它将复制的字符数,这样就不会使缓冲区溢出。谢谢Brian,你的第二个版本对我来说更容易理解,我现在就明白了。谢谢你输入了所有内容,我看看我能从中得到什么,我正在添加字符[][]…一秒钟。哦,我明白了