C 如何检查argv[1]是否以特定字符结尾?
我正在试图找出如何将C 如何检查argv[1]是否以特定字符结尾?,c,C,我正在试图找出如何将argv[1]值与几个测试用例进行比较。我想看看argv[1]是否以特定的char值结尾。到目前为止,我有以下代码: int main(int argc, char * argv[]) { char strin[250]; int length; printf("The argument supplied is %s\n", argv[1]); strcpy(strin,argv[1]); length = strlen(strin); printf("Testing: %c
argv[1]
值与几个测试用例进行比较。我想看看argv[1]
是否以特定的char值结尾。到目前为止,我有以下代码:
int main(int argc, char * argv[])
{
char strin[250];
int length;
printf("The argument supplied is %s\n", argv[1]);
strcpy(strin,argv[1]);
length = strlen(strin);
printf("Testing: %c",strin[length]);
if( strin[length] = 'b')
{
printf("b in the input");
}
}
但出于某种原因,每当我输入任何输入时,print语句都会触发。如何检查命令行参数中的最后一个字符是否等于我将其设置为等于的字符?字符串以0结尾,索引从0开始。但是
strlen()
不计算终止符
因此strin[length]
始终是0-终止符,您需要strin[length-1]
来获取最后一个字符。当然,只有当length>0
为真时,才能这样做
C中的比较是使用=
操作符完成的,单个=
是赋值,这不是您想要的
复制字符串也没有意义,您可以直接签入
argv[1]
;和strlen()
returnsize\u t
,而不是int
C数组是基于零索引的。要访问您执行的第一个元素数组[0]
,要访问您执行的第十个元素数组[9]
printf("Testing: %c",strin[length]);
这将打印字符串中最后一个字符后的字符1,该字符恰好是空终止符\0
。(这就是C字符串的工作原理)
这是不可比较的,您应该改用==
。这也存在与上述相同的问题
因此,将访问权限更改为
[length-1]
,并使用=
,正如其他人所说,字符串中的最后一个字符是strlen()-1。因为您正在使用strcpy,所以您还有另一个问题。如果参数字符串中的字符超过250个,则非常不安全。因此,为了安全起见,您需要使用strncpy
int main(int argc, char * argv[])
{
char strin[250];
int length;
printf("The argument supplied is %s\n", argv[1]);
strncpy(strin,argv[1], 250);
length = strlen(strin);
printf("Testing: %c",strin[length-1]);
if( strin[length] = 'b')
{
printf("b in the input\n");
}
}
基本上,您只需执行以下操作:
int main(int argc, char * argv[]) {
// check if there's an argument to test
if (1 > argc) {
// extract the position of the last character
int last_pos = strlen(argv[1])-1;
// compare the last character with the character "b"
if (0 <= last_pos && 'b' == argv[1][last_pos]) {
printf("Hoora! The input ends with b!");
return 0;
} else {
printf("Bummer… The input does not end with b :(");
}
} else {
printf("there's no argument to test!");
}
}
strin[length]
->strin[length-1]
在您的测试分配中也使用==而不是=:strin[length]='b'
;比较:strin[length]==“b”
为什么要编辑掉代码?如果他只想测试最后一个字符,根本不需要复制。@Barmar是的,但我想让他知道他的代码是危险的。对于索引为零的数组:是的,但是为什么要检查argv[0]
?其价值由发射过程(即外壳)决定。要检查用户提供的参数,您必须从argv[1]
@EliasVanOotegem开始。这不是我的观点,我的观点是OP可能认为[length]会指向length'th元素,而不是-1。您忘了检查argv[1]
不是长度0(last\u pos
可能是负数).0但我就是这么写的
int main(int argc, char * argv[]) {
// check if there's an argument to test
if (1 > argc) {
// extract the position of the last character
int last_pos = strlen(argv[1])-1;
// compare the last character with the character "b"
if (0 <= last_pos && 'b' == argv[1][last_pos]) {
printf("Hoora! The input ends with b!");
return 0;
} else {
printf("Bummer… The input does not end with b :(");
}
} else {
printf("there's no argument to test!");
}
}
int main(int argc, char * argv[])
{
char strin[250];
int length;
printf("The argument supplied is %s\n", argv[1]);
// you're doing a copy from the first argument into the
// variable strin. If argv[1] is 251 characters, you'll
// overwrite memory, and will cause a "buffer overflow".
// Whenever you need to do strcpy of data input by a user
// use strncpy().
strcpy(strin,argv[1]);
// you're extracting the /length/ of the string, not the /position/
// of the last character, so when you're trying to access at index
// length, you'll get data from one character beyond the array.
length = strlen(strin);
// so here you're seeing a random value from the memory of your computer
printf("Testing: %c",strin[length]);
// here you made a mistake and you're assigning the value 'b' to the
// value beyond the allocated memory for the array. Basically:
// Here be dragons.
// To avoid that mistake, always put the value you're comparing against
// in a comparaison on the Left Hand Side, and the value you're comparing
// on the right hand side. Then the compiler will yell at you!
if( strin[length] = 'b')
{
printf("b in the input");
}
}