c中的2d字符数组未正确写入

时间:2015-10-02 01:37:18

标签: c

我是C语言编程的新手,并且还没有习惯没有字符串。下面的代码会将第一个单词正确写入Arguments [0],但所有其他单词都不能正确写入,我不知道为什么。有人能帮助我吗?

#include <stdio.h>
#include <stdbool.h>

int main(int argc, int **argv)
{
    /*Get input and store it in str*/
    char str[100];
    if( fgets(str, 100, stdin) != NULL)
    {

    }
    else
    {
        perror("Null input");
    }

    /*Parse input into separate arguments*/
    int numArg = 0;
    int wordStart = 0;
    int wordEnd = 0;
    bool wordStarted = false;
    char Arguments[30][100];
    /*iterate through str*/
    for(int i =0; i < 100; i++)
    {
        /*if we're reading a character that isn't a space and we aren't parsing a word yet*/
        /*required special case for beginning of input not being a space*/
        if(i==0 && str[i] != ' ' || str[i] != ' ' && wordStarted == false)
        {
            /*set this spot in array to be the start of a word*/
            wordStart = i;
            /*set boolean so that we are parsing a word*/
            wordStarted = true;
        }
        /*if we're parsing a word, and we see a space or see the line end*/
        else if(str[i] == ' ' && wordStarted == true || str[i] == '\n')
        {
            /*set this spot in array to be the end of a word*/
            wordEnd = i;
            /*put word into *Arguments*/
            for(int k = 0; k < (wordEnd - wordStart); k++)
            {
                Arguments[numArg][k+wordStart] = str[k+wordStart];
            }
            /*add null character to end*/
            Arguments[numArg][k+wordStart] = '\0';
            /*increase number of arguments by 1*/
            numArg++;
            /*set boolean so that we are no long parsing a word*/
            wordStarted = false;
        }
    }
    printf("numArg is %d\n", numArg);
    int j = 0;
    for(j; j < numArg; j++)
    {
        printf("Argument %d is: %s\n", j, Arguments[j]);
    }
}

如果我要运行此代码然后输入:

there are four words

输出将是:  

numArg is 4
Argument 0 is: there
Argument 1 is:
Argument 2 is:
Argument 3 is:` 

我无法弄清楚为什么输出不是

numArg is 4
Argument 0 is: there
Argument 1 is: are
Argument 2 is: four
Argument 3 is: words

1 个答案:

答案 0 :(得分:0)

试试这个,并尝试理解代码,看看你的代码无法正常工作的原因,因为有多种原因我认为向你展示一个简单的工作实现会更有帮助

#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>

int main(int argc, char **argv)
{
    char *last;
    char *pointer;
    char text[100];
    int count;
    char words[30][100];

    if ((pointer = fgets(text, sizeof(text), stdin)) == NULL)
        return -1;
    while ((*pointer != '\0') && (isspace((int) *pointer) != 0))
        ++pointer;
    count = 0;
    last = pointer;
    while (*pointer != '\0')
    {
        if (isspace((int) *pointer) != 0)
        {
            size_t length;

            length = pointer - last;
            memcpy(words[count], last, length);

            words[count][length] = '\0';

            last = pointer + 1;
            count += 1;
        }
        ++pointer;
    }

    fprintf(stdout, "There are %d words\n\n", count);
    for (int i = 0 ; i < count ; ++i)
        fprintf(stdout, "\t%d: %s\n", i + 1, words[i]);
}

我不是很小心,所以这可能有很多问题,但这是一种简单的方法来做你想要的,例如我可以想到“ words ”之间的多个空格,这个代码不能正确处理,但正如所说,它会做你想要的或多或少。