将字符串传递给 main 并分解为数组
Pass string to main and break up into array
我正在使用以下代码将参数传递给 main:
#include <stdio.h>
int main(int argc, char ** argv)
{
int i = 1;
for(i = 1; i < argc; i++)
printf("%c", argv[i]);
return 0;
}
所以我使用 ./test 218 abc 392990xFF[w2 dlx
效果很好。但是,数组是:
arr[1] = "218"
arr[2] = "abc"
arr[3] = "392990xFF[w2"
arr[4] = "dlx"
我希望数组是这样的:
arr[0] = '2'
arr[1] = '1'
arr[2] = '8'
arr[3] = 'a'
etc...
如果不在每个数字或字符后加上 space,我怎样才能做到这一点?
确定所有字符串中的字符总数,然后分配一个该长度的新字符数组,然后将输入的字符复制到新数组中。
最后一部分可以利用您在第一部分中收集的大小:对所有参数字符串进行外循环,对每个字符串中的字符进行内循环。
编辑:现在我不在移动设备上,下面是上面的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char ** argv)
{
//For storing sizes of each input string
int *arg_chars;
//Where the individual characters are stored
char *stored_chars;
/* Determine total number of characters, and store
characters in each word for later re-use */
arg_chars = malloc(argc * sizeof(int));
int total_chars = 0;
//Loop starts at 1 since we don't care about arg 0
for(int i=1; i<argc; i+=1)
{
arg_chars[i] = strlen(argv[i]);
total_chars += arg_chars[i];
printf("Word %d is %d long\n", i, arg_chars[i]);
}
/* Load argument characters into the stored_chars array */
stored_chars = malloc(total_chars * sizeof(char));
int current_char = 0;
//Loop starts at 1 to exclude the program name (arg 0)
for(int i = 1; i < argc; i+=1)
{
printf("Scanning word %d (%s):\n", i, argv[i]);
for(int j = 0; j < arg_chars[i]; j+=1)
{
stored_chars[current_char] = argv[i][j];
printf(" Stored letter %d `%c` (letter %d of word %d)\n", current_char, argv[i][j], j, i);
current_char += 1;
}
}
/* Demonstrate that it's all loaded and accessible in any order */
for(int i=total_chars-1; i >= 0; i-=1)
{
printf("stored_chars[%d] = `%c`\n", i, stored_chars[i]);
}
return 0;
}
Output:
Word 1 is 3 long
Word 2 is 3 long
Word 3 is 12 long
Word 4 is 3 long
Scanning word 1 (218):
Stored letter 0 `2` (letter 0 of word 1)
Stored letter 1 `1` (letter 1 of word 1)
Stored letter 2 `8` (letter 2 of word 1)
Scanning word 2 (abc):
Stored letter 3 `a` (letter 0 of word 2)
Stored letter 4 `b` (letter 1 of word 2)
Stored letter 5 `c` (letter 2 of word 2)
Scanning word 3 (392990xFF[w2):
Stored letter 6 `3` (letter 0 of word 3)
Stored letter 7 `9` (letter 1 of word 3)
Stored letter 8 `2` (letter 2 of word 3)
Stored letter 9 `9` (letter 3 of word 3)
Stored letter 10 `9` (letter 4 of word 3)
Stored letter 11 `0` (letter 5 of word 3)
Stored letter 12 `x` (letter 6 of word 3)
Stored letter 13 `F` (letter 7 of word 3)
Stored letter 14 `F` (letter 8 of word 3)
Stored letter 15 `[` (letter 9 of word 3)
Stored letter 16 `w` (letter 10 of word 3)
Stored letter 17 `2` (letter 11 of word 3)
Scanning word 4 (d1x):
Stored letter 18 `d` (letter 0 of word 4)
Stored letter 19 `1` (letter 1 of word 4)
Stored letter 20 `x` (letter 2 of word 4)
stored_chars[20] = `x`
stored_chars[19] = `1`
stored_chars[18] = `d`
stored_chars[17] = `2`
stored_chars[16] = `w`
stored_chars[15] = `[`
stored_chars[14] = `F`
stored_chars[13] = `F`
stored_chars[12] = `x`
stored_chars[11] = `0`
stored_chars[10] = `9`
stored_chars[9] = `9`
stored_chars[8] = `2`
stored_chars[7] = `9`
stored_chars[6] = `3`
stored_chars[5] = `c`
stored_chars[4] = `b`
stored_chars[3] = `a`
stored_chars[2] = `8`
stored_chars[1] = `1`
stored_chars[0] = `2`
运行 时间环境传递给程序的参数可以被main
仅使用int argc, char** argv
捕获。如果您需要将它们组合成一个大数组,则需要为此编写代码,或者一次打印一个字符。
int main(int argc, char ** argv)
{
int i = 1;
int j;
int len;
for(i = 1; i < argc; i++)
{
len = strlen(argv[i]);
for ( j = 0; j < len; ++j )
{
printf("%c", argv[i][j]);
}
}
return 0;
}
首先这不是它要打印的内容 -
arr[0] = "218"
arr[1] = "abc"
arr[2] = "392990xFF[w2"
arr[3] = "dlx"
argv[0]
将存储 ./test
。 "218"
将在索引 1
上,因此其他类似。
还有 printf("%c", argv[i]);
.%c
需要一个 char
而你传递了一个不正确的字符串。
解决方案可能是-
#include <stdio.h>
int main(int argc, char ** argv)
{
int i = 1,j;
for(i = 1; i <argc; i++)
for(j=0;argv[i][j]!='[=11=]';j++)
printf("%c\n", argv[i][j]);
return 0;
}
除了 for
循环,您还可以简单地使用指针和 while
循环。 C:
解决问题的方法一般有很多
#include <stdio.h>
int main (int argc, char **argv) {
int i = 1;
int j = 0;
while (i < argc) {
char *p = argv[i];
while (*p) {
printf (" arr[%2d] = \"%c\"\n", j++, *p);
p++;
}
i++;
}
return 0;
}
输出
$ ./bin/argvchars 218 abc 392990xFF[w2 dlx
arr[ 0] = "2"
arr[ 1] = "1"
arr[ 2] = "8"
arr[ 3] = "a"
arr[ 4] = "b"
arr[ 5] = "c"
arr[ 6] = "3"
arr[ 7] = "9"
arr[ 8] = "2"
arr[ 9] = "9"
arr[10] = "9"
arr[11] = "0"
arr[12] = "x"
arr[13] = "F"
arr[14] = "F"
arr[15] = "["
arr[16] = "w"
arr[17] = "2"
arr[18] = "d"
arr[19] = "l"
arr[20] = "x"
我正在使用以下代码将参数传递给 main:
#include <stdio.h>
int main(int argc, char ** argv)
{
int i = 1;
for(i = 1; i < argc; i++)
printf("%c", argv[i]);
return 0;
}
所以我使用 ./test 218 abc 392990xFF[w2 dlx
效果很好。但是,数组是:
arr[1] = "218"
arr[2] = "abc"
arr[3] = "392990xFF[w2"
arr[4] = "dlx"
我希望数组是这样的:
arr[0] = '2'
arr[1] = '1'
arr[2] = '8'
arr[3] = 'a'
etc...
如果不在每个数字或字符后加上 space,我怎样才能做到这一点?
确定所有字符串中的字符总数,然后分配一个该长度的新字符数组,然后将输入的字符复制到新数组中。
最后一部分可以利用您在第一部分中收集的大小:对所有参数字符串进行外循环,对每个字符串中的字符进行内循环。
编辑:现在我不在移动设备上,下面是上面的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char ** argv)
{
//For storing sizes of each input string
int *arg_chars;
//Where the individual characters are stored
char *stored_chars;
/* Determine total number of characters, and store
characters in each word for later re-use */
arg_chars = malloc(argc * sizeof(int));
int total_chars = 0;
//Loop starts at 1 since we don't care about arg 0
for(int i=1; i<argc; i+=1)
{
arg_chars[i] = strlen(argv[i]);
total_chars += arg_chars[i];
printf("Word %d is %d long\n", i, arg_chars[i]);
}
/* Load argument characters into the stored_chars array */
stored_chars = malloc(total_chars * sizeof(char));
int current_char = 0;
//Loop starts at 1 to exclude the program name (arg 0)
for(int i = 1; i < argc; i+=1)
{
printf("Scanning word %d (%s):\n", i, argv[i]);
for(int j = 0; j < arg_chars[i]; j+=1)
{
stored_chars[current_char] = argv[i][j];
printf(" Stored letter %d `%c` (letter %d of word %d)\n", current_char, argv[i][j], j, i);
current_char += 1;
}
}
/* Demonstrate that it's all loaded and accessible in any order */
for(int i=total_chars-1; i >= 0; i-=1)
{
printf("stored_chars[%d] = `%c`\n", i, stored_chars[i]);
}
return 0;
}
Output:
Word 1 is 3 long Word 2 is 3 long Word 3 is 12 long Word 4 is 3 long Scanning word 1 (218): Stored letter 0 `2` (letter 0 of word 1) Stored letter 1 `1` (letter 1 of word 1) Stored letter 2 `8` (letter 2 of word 1) Scanning word 2 (abc): Stored letter 3 `a` (letter 0 of word 2) Stored letter 4 `b` (letter 1 of word 2) Stored letter 5 `c` (letter 2 of word 2) Scanning word 3 (392990xFF[w2): Stored letter 6 `3` (letter 0 of word 3) Stored letter 7 `9` (letter 1 of word 3) Stored letter 8 `2` (letter 2 of word 3) Stored letter 9 `9` (letter 3 of word 3) Stored letter 10 `9` (letter 4 of word 3) Stored letter 11 `0` (letter 5 of word 3) Stored letter 12 `x` (letter 6 of word 3) Stored letter 13 `F` (letter 7 of word 3) Stored letter 14 `F` (letter 8 of word 3) Stored letter 15 `[` (letter 9 of word 3) Stored letter 16 `w` (letter 10 of word 3) Stored letter 17 `2` (letter 11 of word 3) Scanning word 4 (d1x): Stored letter 18 `d` (letter 0 of word 4) Stored letter 19 `1` (letter 1 of word 4) Stored letter 20 `x` (letter 2 of word 4) stored_chars[20] = `x` stored_chars[19] = `1` stored_chars[18] = `d` stored_chars[17] = `2` stored_chars[16] = `w` stored_chars[15] = `[` stored_chars[14] = `F` stored_chars[13] = `F` stored_chars[12] = `x` stored_chars[11] = `0` stored_chars[10] = `9` stored_chars[9] = `9` stored_chars[8] = `2` stored_chars[7] = `9` stored_chars[6] = `3` stored_chars[5] = `c` stored_chars[4] = `b` stored_chars[3] = `a` stored_chars[2] = `8` stored_chars[1] = `1` stored_chars[0] = `2`
运行 时间环境传递给程序的参数可以被main
仅使用int argc, char** argv
捕获。如果您需要将它们组合成一个大数组,则需要为此编写代码,或者一次打印一个字符。
int main(int argc, char ** argv)
{
int i = 1;
int j;
int len;
for(i = 1; i < argc; i++)
{
len = strlen(argv[i]);
for ( j = 0; j < len; ++j )
{
printf("%c", argv[i][j]);
}
}
return 0;
}
首先这不是它要打印的内容 -
arr[0] = "218"
arr[1] = "abc"
arr[2] = "392990xFF[w2"
arr[3] = "dlx"
argv[0]
将存储 ./test
。 "218"
将在索引 1
上,因此其他类似。
还有 printf("%c", argv[i]);
.%c
需要一个 char
而你传递了一个不正确的字符串。
解决方案可能是-
#include <stdio.h>
int main(int argc, char ** argv)
{
int i = 1,j;
for(i = 1; i <argc; i++)
for(j=0;argv[i][j]!='[=11=]';j++)
printf("%c\n", argv[i][j]);
return 0;
}
除了 for
循环,您还可以简单地使用指针和 while
循环。 C:
#include <stdio.h>
int main (int argc, char **argv) {
int i = 1;
int j = 0;
while (i < argc) {
char *p = argv[i];
while (*p) {
printf (" arr[%2d] = \"%c\"\n", j++, *p);
p++;
}
i++;
}
return 0;
}
输出
$ ./bin/argvchars 218 abc 392990xFF[w2 dlx
arr[ 0] = "2"
arr[ 1] = "1"
arr[ 2] = "8"
arr[ 3] = "a"
arr[ 4] = "b"
arr[ 5] = "c"
arr[ 6] = "3"
arr[ 7] = "9"
arr[ 8] = "2"
arr[ 9] = "9"
arr[10] = "9"
arr[11] = "0"
arr[12] = "x"
arr[13] = "F"
arr[14] = "F"
arr[15] = "["
arr[16] = "w"
arr[17] = "2"
arr[18] = "d"
arr[19] = "l"
arr[20] = "x"