我正在尝试用C编写一个程序,该程序需要10个命令行参数并对它们执行某些操作。我已经有了接受10个参数int main(int argc, char **argv)
的部分以及在用户printf(" %s", argv[i]);
输入所有10个参数后输出结果的部分。
我只想弄清楚如何在输入的每个命令行参数上执行操作,即:
(*&%^’$+_
。最后:
764
,则该参数将被第7个arg替换。修改:更新了以下更好的示例
以下是一个示例输入(10个用户输入的参数):
sda 789 io90 poi 4kl24PP +df_1JK MN BV XC __5555
输出应该是什么:
sda MN XC poi poi sda MN BV XC klPP
(还要注意789如何映射到第7个输出,即MN)
答案 0 :(得分:2)
所以这是我的评论作为答案:搜索每个参数中的第一个数字。如果已找到,请将该参数替换为第n个arg的符号和无数副本,否则将其替换为自身的符号和数字副本。 C99实施:
int main(int argc, char *argv[])
{
char *copies[argc - 1];
for (int i = 1; i < argc; i++) {
size_t p = strcspn(argv[i], "0123456789");
int n = argv[i][p] ? argv[i][p] - '0' : i;
if (n == 0) n = 10;
char *copyee = argv[n];
size_t l = strlen(copyee);
copies[i - 1] = malloc(l + 1);
char *copy = copies[i - 1];
for (; *copyee; copyee++) {
if (isalpha(*copyee)) {
*copy++ = *copyee;
}
}
*copy = 0;
}
for (int i = 0; i < argc - 1; i++) {
printf("%s ", copies[i]);
free(copies[i]);
}
printf("\n");
return 0;
}
答案 1 :(得分:0)
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct inputcmd {
char *cmd;
int index;
}inputcmd;
int main (int argc, char *argv[]){
struct inputcmd icmd[10] = {0};
char *newargv[10];
int i;
char *p, *s;
if(argc!=11) {printf("You should input 10 commands\n"); return;}
for (i=1; i<11; i++)
{
icmd[i-1].cmd = calloc(strlen(argv[i])+1, sizeof(char));
p=argv[i]; s=icmd[i-1].cmd;
while(*p) {
if(isalpha(*p))
*s++=*p;
else if ((*p>='0' && *p<='9') && icmd[i-1].index==0) {
if (*p=='0') icmd[i-1].index = 10;
else icmd[i-1].index = *p - '0';
}
p++;
}
}
for (i=0; i<10; i++) {
if (icmd[i].index) newargv[i] = strdup(icmd[icmd[i].index - 1].cmd);
else newargv[i] = strdup(icmd[i].cmd);
}
for (i=0; i<10; i++) {
free(icmd[i].cmd);
}
for (i=0; i<10; i++) {
printf("%s ",newargv[i]);
}
printf("\n");
}
执行1 :
$ ./test sda 789 io90 poi 4kl24PP +df_1JK MN BV XC __5555
sda MN XC poi poi sda MN BV XC klPP
执行2 :
$ ./test a2b c2d e2f g2h i2j k2l l2m n2n o2p q2r
cd cd cd cd cd cd cd cd cd cd
答案 2 :(得分:0)
H2CO3比我的小一点,但这是你问题的另一个快速实现。
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv) {
char **original = argv + 1;
char *messages[10] = {0};
if (argc != 11) {
printf("We expect 10 arguments.\n");
return -1;
}
// Allocate space for the copied data.
for (int i=0; i<10; ++i)
messages[i] = calloc(sizeof(char), strlen(original[i]) + 1);
// Let's parse out the parts of the mutable array that we don't want to keep.
// So kill everything but letters
for (int i=0; i<10; ++i) {
int head = 0;
int tail = 0;
while (original[i][tail] != '\0') {
if (isalpha(original[i][tail])) {
messages[i][head] = original[i][tail];
head++; tail++;
} else {
tail++;
}
}
}
// Now, let's parse each message, and see what we are supposed to print.
for (int i=0; i<10; ++i) {
char *pos = strpbrk(original[i], "0123456789");
if (pos == NULL) {
printf("%s ", messages[i]);
} else {
int index = *pos - '0';
if (index == 0)
index = 10;
printf("%s ", messages[index - 1]);
}
}
for (int i=0; i<10; ++i)
free(messages[i]);
printf("\n");
}