在C中从命令行对字符串进行复数化
此文件已编译并运行,但出现错误。这是一个家庭作业,我已经基本完成了,但输出不正确。我不是要求对我的程序进行重新设计,而是对我做错了什么给出一个有用的提示。错误就在输出中,每隔一个单词就会出错,我不知道为什么。谢谢你的帮助在C中从命令行对字符串进行复数化,c,C,此文件已编译并运行,但出现错误。这是一个家庭作业,我已经基本完成了,但输出不正确。我不是要求对我的程序进行重新设计,而是对我做错了什么给出一个有用的提示。错误就在输出中,每隔一个单词就会出错,我不知道为什么。谢谢你的帮助 #include <stdio.h> #include <string.h> #define SIZE 20 void plural(char word[]); int main(int argc, char *argv[]) { if(a
#include <stdio.h>
#include <string.h>
#define SIZE 20
void plural(char word[]);
int main(int argc, char *argv[])
{
if(argc >= 2)
{
int i;
for(i=1; i<argc; i++)
{
printf("noun: %s\n",argv[i]);
plural(argv[i]);
printf("plural: %s\n\n", argv[i]);
}
}
else
{
printf("ERROR: You must pass the nouns to be pluralized as program arguements\n");
}
return 0;
}
void plural(char word[])
{
/* declarations */
int length;
/* find length of word */
length = strlen(word);
/* check first rule: if word ends in "y" then change to "ies" */
if (word[length - 1] == 'y') {
word[length - 1] = 'i';
word[length] = 'e';
word[length + 1] = 's';
word[length + 2] = '\0'; /* put '\0' at end of string */
}
/* check second rule: if word ends in "s" "ch" or "sh" add "es" */
else if (word[length - 1] == 's' ||
(word[length - 2] == 'c' && word[length - 1] == 'h') ||
(word[length - 2] == 's' && word[length - 1] == 'h'))
{
/* concatenate "es" to word */
strcat(word, "es");
}
/* otherwise, just add "s" to the end of word */
else
{
strcat(word, "s");
}
}
#包括
#包括
#定义尺寸20
无效复数(字符词[]);
int main(int argc,char*argv[])
{
如果(argc>=2)
{
int i;
对于(i=1;i每个argv[i]
都有strlen(argv[i])+1
分配给它的内存。您写入更多的字符,这些字符进入无效的内存位置,调用未定义的行为
修复方法是根据需要从函数和malloc
内存中创建一个指针,返回指针并从main
释放它
请参阅此代码(未测试):
#包括
#包括
#包括
#定义尺寸20
/*注:尺寸未使用*/
字符*复数(字符词[]);
int main(int argc,char*argv[])
{
如果(argc>=2)
{
int i;
对于(i=1;i1&&word[length-2]='c'&&word[length-1]='h')||
(长度>1&&word[length-2]='s'&&word[length-1]='h'))
{
str=malloc(长度+3);
strcpy(str,word);
/*将“es”连接到单词*/
strcat(str,“es”);
}
/*否则,只需在单词末尾加上“s”*/
其他的
{
str=malloc(长度+2);
strcpy(str,word);
strcat(str,“s”);
}
返回str;
}
您不应将argv
参数列表当作可修改的字符串来使用。最重要的是,您不知道为它们分配了多少超出其长度的内存,因此您不能使用strcat
之类的东西,它将创建数组越界错误
相反,在修改这些参数之前,请制作一份硬拷贝。您不能在字符串的长度之外写入。word[length+1]
和word[length+2]
是错误的。您应该使用一个新的更大的字符串来存储复数
您只需将argv复制到一个新的较大字符串中:
#define SIZE 128
void plural(char word[]);
int main(int argc, char *argv[])
{
char plural_string[SIZE] = {0};
if(argc >= 2)
{
int i;
for(i=1; i<argc; i++)
{
printf("noun: %s\n",argv[i]);
strncpy(plural_string, argv[i], SIZE);
plural(plural_string);
printf("plural: %s\n\n", plural_string);
}
}
else
{
printf("ERROR: You must pass the nouns to be pluralized as program arguements\n");
}
return 0;
}
#定义大小128
无效复数(字符词[]);
int main(int argc,char*argv[])
{
字符复数_字符串[SIZE]={0};
如果(argc>=2)
{
int i;
对于(i=1;i您是说要为输入的值创建一个新数组并使用该数组吗?@TAG如果您需要修改值,那么是的。一个指针数组,其中每个指针指向一个字符串可能是最好的选择,因为您的程序在编译时无法知道字符串长度。您不能在字符串的长度lenght
。word[length+1]
和word[length+2]
是错误的。您应该使用一个新的更大的字符串来存储复数。事实上。虽然C标准规定argv字符串是可修改的,但它们仍然分配了固定长度的内存-strlen(argv[n])+1
字节,所以您不能写超过这个字节。同样的情况也适用于strcat
。如果长度
,程序将因字[length-2]
而崩溃2@Cool盖伊,你用了char*复数(char-word[])
声明但忘记返回char*
@AbdulGafoor-Whoops。修复。Thanks@AlterMann很好的捕获。谢谢。编辑。@Cool Guy,strcpy之后的最后一个else语句中缺少分号。除此之外,代码运行良好,除了有y的单词,y永远不会被删除,它只是添加了y。例如,fly变成flyies。我很好ng:word[length-1]
是否会越界?我的意思是,length
将始终>=1,因为我们处理的是argv[I]
,对吗?@LPs感谢您的帮助,并保持我的大部分代码不变。如果我在If-else语句之外以复数形式放置If语句()检查If(length>2)这应该考虑到超出范围吗?选择128作为大小定义是否有理由?@TAG128
足够大,可以存储最大的单词。35
也可以存储SuperCalifragilisticExpialidious。if(长度>2)
检查传递的字符串应该没问题。@LPs再次感谢您今晚的帮助,由于您的输入,代码现在可以正常工作了!
#define SIZE 128
void plural(char word[]);
int main(int argc, char *argv[])
{
char plural_string[SIZE] = {0};
if(argc >= 2)
{
int i;
for(i=1; i<argc; i++)
{
printf("noun: %s\n",argv[i]);
strncpy(plural_string, argv[i], SIZE);
plural(plural_string);
printf("plural: %s\n\n", plural_string);
}
}
else
{
printf("ERROR: You must pass the nouns to be pluralized as program arguements\n");
}
return 0;
}