将字符串传递给main并分解为数组

时间:2015-09-04 03:52:11

标签: c arrays character c-strings

我使用此代码将参数传递给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...

如何在不在每个数字或字符后面留出空格的情况下实现此目的?

4 个答案:

答案 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;

}

答案 1 :(得分:2)

首先,这不是它打印的内容 -

    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]!='\0';j++)
              printf("%c\n", argv[i][j]);

       return 0;
 }

答案 2 :(得分:1)

确定所有字符串中的字符总数,然后分配该长度的新字符数组,然后将输入字符复制到新数组中。

最后一部分可以利用你在第一部分中收集的大小:在所有参数字符串上都有一个外部循环,每个字符串中的字符都有一个内部循环。

编辑:既然我不在移动设备上,请参阅上面的代码:

#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;
}
  

输出:

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`

答案 3 :(得分:1)

而不是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"