C 如何计算存储在二维数组中的单词数(添加另一个单词后)?
这是我的代码:C 如何计算存储在二维数组中的单词数(添加另一个单词后)?,c,C,这是我的代码: #include <stdio.h> #include <string.h> int main() { char Words[][20] = {"Dog", "Cat", "Table"}; char command[20]; do { printf("\nEnter command(Add, End): ");
#include <stdio.h>
#include <string.h>
int main()
{
char Words[][20] = {"Dog", "Cat", "Table"};
char command[20];
do
{
printf("\nEnter command(Add, End): ");
/* input should be "Add" , write down a word, then "Add" again, then write down a new word. */
scanf("%s", &command);
if (strcmp(command, "Add") == 0)
{
int NumberOfWords = sizeof(Words) / sizeof(Words[0]);
char NewWord[20];
printf("Enter new word: ");
scanf("%s", &NewWord);
memcpy(Words[NumberOfWords], NewWord, sizeof(Words[NumberOfWords]));
printf("\nTEST- the number of words is: %d\n" , NumberOfWords);
}
}while (strcmp(command, "End") != 0);
printf("Program has ended!");
return 0;
}
#包括
#包括
int main()
{
字符词[][20]={“狗”、“猫”、“表”};
char命令[20];
做
{
printf(“\n输入命令(添加,结束):”;
/*输入应该是“添加”,写下一个单词,然后再“添加”,然后写下一个新单词*/
scanf(“%s”,命令(&C);
如果(strcmp(命令,“添加”)==0)
{
int NumberOfWords=sizeof(Words)/sizeof(Words[0]);
char新词[20];
printf(“输入新单词:”);
scanf(“%s”和NewWord);
memcpy(单词[NumberOfWords]、NewWord、sizeof(单词[NumberOfWords]);
printf(“\n测试-字数为:%d\n”,NumberOfWords);
}
}while(strcmp(命令,“结束”)!=0);
printf(“程序已结束!”);
返回0;
}
问题是,NumberOfWords
始终保持在3(也就是说,字数始终是3),即使假定为words[3]
增加了值(这应该使字数=4)。这使得向数组中添加更多的单词成为不可能
如何解决这个问题?要做到这一点,您需要在两个选项中进行选择,创建一个巨大的数组来存储可能添加的所有类型的动物,或者使用动态数组和内存分配。在下面的代码中,我使用内存分配。
基本上,我创建了一个指向字符指针的指针,也就是一个字符串数组,并为其中3个字符串分配空间。我继续将初始字符串复制到这些位置。之后,在每个赛程中,您必须使用realloc来增加分配空间的大小。
请记住,我没有检查malloc/realloc中的错误,但您应该始终这样做。另外,另一个好的做法是在最后释放内存。这个代码就是这样做的 编辑:忘记提到“scanf(“%s”,&command);”是不好的。这会生成警告,因为命令已经是指针
#包括
#包括
#包括
int main()
{
字符**字=(字符**)malloc(3*sizeof(字符*);
char命令[20];
int NumberOfWords=3;
字[0]=(char*)malloc(sizeof(“狗”)+1);
strcpy(单词[0],“Dog”);
字[1]=(char*)malloc(sizeof(“Cat”)+1);
strcpy(词语[1],“Cat”);
字[2]=(char*)malloc(sizeof(“表”)+1);
strcpy(文字[2],“表格”);
做
{
printf(“\n输入命令(添加,结束):”;
/*输入应该是“添加”,写下一个单词,然后再“添加”,然后写下一个新单词*/
scanf(“%s”,命令);
如果(strcmp(命令,“添加”)==0)
{
char新词[20];
printf(“输入新单词:”);
scanf(“%s”,新词);
NumberOfWords++;
单词=realloc(单词、大小(字符*)*单词数);
单词[NumberOfWords-1]=(char*)malloc(sizeof(NewWord)+1);
strcpy(单词[NumberOfWords-1],新词);
对于(int i=0;i要做到这一点,您需要在两个选项中进行选择,创建一个巨大的数组来存储可能添加的所有类型的动物,或者使用动态数组和内存分配。在下面的代码中,我使用内存分配。
基本上,我创建一个指向字符指针的指针,也就是一个字符串数组,并为其中3个字符串分配空间。我继续将初始字符串复制到这些位置。之后,在每个循环中,您必须使用realloc来增加分配空间的大小。
请记住,我不是在检查malloc/realloc中的错误,但您应该始终这样做。另外,另一个好的做法是在最后释放内存。这段代码就是这样做的
编辑:忘记提到“scanf(“%s”,&command);”是不好的。这会生成一个警告,因为command已经是指针了
#包括
#包括
#包括
int main()
{
字符**字=(字符**)malloc(3*sizeof(字符*);
char命令[20];
int NumberOfWords=3;
字[0]=(char*)malloc(sizeof(“狗”)+1);
strcpy(单词[0],“Dog”);
字[1]=(char*)malloc(sizeof(“Cat”)+1);
strcpy(词语[1],“Cat”);
字[2]=(char*)malloc(sizeof(“表”)+1);
strcpy(文字[2],“表格”);
做
{
printf(“\n输入命令(添加,结束):”;
/*输入应该是“添加”,写下一个单词,然后再“添加”,然后写下一个新单词*/
scanf(“%s”,命令);
如果(strcmp(命令,“添加”)==0)
{
char新词[20];
printf(“输入新单词:”);
scanf(“%s”,新词);
NumberOfWords++;
单词=realloc(单词、大小(字符*)*单词数);
单词[NumberOfWords-1]=(char*)malloc(sizeof(NewWord)+1);
strcpy(单词[NumberOfWords-1],新词);
对于(int i=0;i如果要动态增加字数,可以使用char**
并根据需要进行存储
在下面的示例中(这是您代码的修改版本),我将自己限制为仅5个单词,然后显示和释放
您可以根据需要增加MAX_WORDS
我在w.r.tmemcpy
和scanf
阅读中做了更多的修正
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_WORDS 5
int main()
{
//char Words[][20] = {"Dog", "Cat", "Table"};
char **Words = NULL;//[][20] = {"Dog", "Cat", "Table"};
char command[20];
int NumberOfWords = 0;
if( ( Words = malloc(MAX_WORDS * sizeof (char*))) == NULL)
{
printf("malloc failed");
return -1;
}
do
{
printf("\nEnter command(Add, End): ");
/* input should be "Add" , write down a word, then "Add" again, then write down a new word. */
if(1 != scanf("%s", command)) {
printf("scanf error\n");
//break to free memory in any allocated
break;
}
if (strcmp(command, "Add") == 0)
{
//sizeof(Words) / sizeof(Words[0]);
char NewWord[20];
printf("Enter new word: ");
if(1 != scanf("%s", NewWord)) {
printf("scanf error\n");
//break to free memory in any allocated
break;
}
if( ( Words[NumberOfWords] = malloc(strlen(NewWord) +1)) == NULL)
{
printf("malloc failed for NewWord alloc\n");
break;
}
strcpy(Words[NumberOfWords], NewWord);//, sizeof(Words[NumberOfWords]));
// increase NumberOfWords
NumberOfWords += 1;
printf("\nTEST- the number of words is: %d\n" , NumberOfWords);
if(MAX_WORDS == NumberOfWords) {
printf(" max words reached");
break;
}
}
}while (strcmp(command, "End") != 0);
printf("\n words are: ");
for(int i = 0; i< NumberOfWords; i++)
{
printf("%s ", Words[i]);
free(Words[i]);
Words[i] = NULL;
}
free(Words);
Words = NULL;
printf("Program has ended!");
return 0;
}
#包括
#包括
#包括
#定义最大字数5
int main()
{
//字符词[][20]={“狗”、“猫”、“表”};
字符**Words=NULL;//[[20]={“狗”、“猫”、“表”};
char命令[20];
int NumberOfWords=0;
if((Words=malloc(MAX_Words*sizeof(char*)))==NULL)
{
printf(“malloc失败”);
返回-1;
}
做
{
printf(“\n输入命令(添加,结束):”;
/*输入应该是“添加”,写下一个单词,然后再“添加”,然后写下一个新单词*/
如果(1!=scanf(“%s”,命令)){
printf(“扫描错误\n”);
//中断以释放任何已分配内存
打破
}
如果(strcmp(命令,“添加”)==0)
{
//sizeof(字)/sizeof(字[0]);
char新词[20];
printf(“输入新单词:”);
如果(1!=scanf(“%s”,新词)){
printf(“sca