以下是一个简单的代码,它接受一个命令行参数,当然不是程序名,而是检查它是否是1到32之间的整数。
然后它等待另一个输入。我怎样检查这个,就像我检查过字母一样?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if (argc == 1)
{
printf("\nPlease enter a cmd-line arg between 1 and 32.\n");
return 0;
}
if (argc == 2)
{
int x;
char *ptr;
long y = strtol(argv[1], &ptr , 10);
int l = strlen(argv[1]);
for (x = 0; x <= (l - 1); x++)
{
if (argv[1][x] >= 'a' && argv[1][x] <= 'z')
{
printf("\nYou can't enter letters.\n");
return 0;
}
else if (argv[1][x] >= 'A' && argv[1][x] <= 'Z')
{
printf("\nYou can't enter letters.\n");
return 0;
}
else
{
//printf("\nCeaser shift accepted.\n");
}
}
if (y <= 0 || y > 32)
{
printf("\nCeaser shift must be between 1 and 32.\n", y);
return 0;
}
}
if (argc > 2)
{
printf("You entered too many arg.");
return 0;
}
printf("\nYour arg is accepted.\n");
return 0;
}
答案 0 :(得分:2)
这是特殊的控制台标志(Escape Character [1,2]),你什么也做不了。
你可以这样运行你的程序:
myCode "^" # (with double quotes)
或
myCode ^^