【发布时间】:2020-03-29 22:43:15
【问题描述】:
int main(int argc, string argv[]) {
int ln = strlen (argv['\0']);
int count = 0;
char cipher_keyword [count+1];
for (int i = 1; i < argc; i++) {
for (int j = 0, n = strlen(argv[i]); j < n; j++){
cipher_keyword [j] = argv [i][j];
printf("Cipher_keyword: %c\n", cipher_keyword [j]);
}
}
printf("Cipher_keyword_outofLoop: %s\n", cipher_keyword);
printf("\nCount of input string: %d\n", count);
return 0;
}
输入是:
argv (file, arg1, arg2, arg3);
例如:
argv (file, abc defg hijkl)
现在,当我在循环中打印 cipher_keyword [j] 时,我将逐行打印字符串的每个元素(这是预期的)。我希望将其存储在cipher_keyword 中,循环外的printf 命令应该将所有元素放在一行中,没有任何空格。但是在循环外的printf 命令中,cipher_keyword 给了我[str.length][j] 的结果,即jijkl。
如何使循环外的printf 命令打印所有元素,即abcdefghijkl?
【问题讨论】:
-
另外我不认为你可以做 int ln = strlen (argv['\0']);您应该在 [] 括号中传递一个 int,例如 argv[0]
标签: c++ c arrays string substring