【发布时间】:2015-11-05 00:43:50
【问题描述】:
我正在尝试设置一个函数来查看一串文本,并将“y”替换为“ies”以使其复数。
我在这里遇到的问题(除了无知)是该函数不会进入第一个嵌套的 if 语句。 (
char noun_to_plural(char n[10])
{
int length;
char temp[10];
char replace[10];
length = strlen(n); //This returns 3 correctly when passed "fly"
printf("length is %d\n", length );
if (length == 3 ) //Successfully enters this statement
{
printf("1st Loop Entered\n");
strcpy(temp, &n[length -1]); //correctly retuns "y" for value temp.
printf("Temp value is %s\n", temp);
if (temp == 'y') //It will not pass into this if condition even
//though temp is 'y'
{
printf("2nd Loop Entered");
replace[10] = strcpy(replace, n );
replace[3] = 'i';
replace[4] = 'e';
replace[5] = 's';
printf("Loop entered test-%s-test", n ); //returns string "fly"
}
}
}
最后,有没有更简单的方法可以将我缺少的“y”更改为“ies”? 这个功能显然不完整,因为我正在努力让它进入第二种状态。我什至尝试使用:
if (strcpy(temp, &n[length -1] == 'y')
那也没用。
【问题讨论】:
-
您没有收到
if (temp == 'y')的编译器警告,如下所示?还有replace[10] = strcpy(replace, n );