如何分离数组并将其存储在新数组中?

时间:2013-12-19 08:24:40

标签: c arrays string strcpy

我想要做的是:用户输入带逗号的字符串;例如:123456,44,55,,66

我希望将它分开并存储在没有逗号的新数组中;例如:

m[0][]={123456}, m[1][]={44}, m[2][]={55}, m[3][]={}, m[4][]={66}

123456是学生ID号,44是第一个模块的标记,55是第二个模块的标记,NULL表示学生没有取第3个模块,66是第4个模块的标记。

我怎样才能做到这一点?我所知道的是,通过检测双重逗号,这意味着学生没有参加第3个模块。

这是我到目前为止所写的内容:

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

void copystring(char m[],char temp[]);

main()
{
    char temp[10000];
    char m[10000][10000];
    gets(temp);
    copystring(m,temp);
    printf("%s\n",m);           
    return 0;
}

void copystring(char m[],char temp[])
{
    int i;
    int j;
    for (j=0;j<(strlen(temp));j++)
    {
        for (i=0;i<(strlen(temp));i++)
        {
            if (temp[i]!=*(",")&&temp[i]!=*(" "))
            {
                m[j][i]=temp[i];
            }   
        }
    }
}

5 个答案:

答案 0 :(得分:1)

我已编辑了我的代码,以便您可以了解如何声明包含2D数组作为参数的函数原型。也可以使用fgets()而不是gets()。该函数返回读取的标记数,即整数。我认为这可能有所帮助。运行代码并查看手册页或google以更好地理解fgets()。

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 1000
int stringcopy(char m[][SIZE],char temp[]);
main()
{
        char temp[SIZE],m[100][SIZE];
        fgets(temp,SIZE,stdin);
        int num=stringcopy(m,temp);
        int i;
        for(i=0;i<num;++i)
                printf("%s\n",m[i]);
        return 0;
}
int stringcopy(char m[][SIZE],char temp[]) {
        int len=strlen(temp);
        int i,j=0,k=0;
        for(i=0;i<len;++i) {
                if(temp[i]!=',')
                        m[j][k++]=temp[i];
                else {
                        m[j][k]='\0';
                        ++j;
                        k=0;
                }
        }
        m[j][k-1]='\0';
        return j+1;
}

答案 1 :(得分:1)

您必须以不同的步骤解决问题,

第一步是检查输入字符串中有多少个标记,以便能够分配足够的空间来存储标记数组。

然后你应该在你的标记字符串数组中提取输入字符串的标记。要从输入字符串中提取标记,您可以使用strtok中的<string.h>函数。

最后,您可以根据需要使用令牌,例如在您的情况下将其转换为long

编辑:根据要求,这里是您可以做的一个小实现。我不检查malloc的返回,你可能应该这样做。

int main(int argc, char** argv) {
    int i;

    char* input_string = /* your input string for somewhere */;

    char** tokens;
    int tokens_count = 0;
    char* input_ptr = input_string;

    char* tmp_token;
    size_t tmp_token_length;

    // count the occurences of your separtor to have the number of elements
    for(; input_ptr[tokens_count]; input_ptr[tokens_count] == ',' ? tokens_count++ : input_ptr++);

    if(tokens_count == 0) {
        // no tokens found, what do you want to do here ?
    }
    else {
        // build our tokens array
        tokens = malloc(sizeof(*tokens) * tokens_count);
        i = 0;

        tmp_token = strtok(input_string, ',');
        while(tmp_token != NULL) {
            tmp_token_length = strlen(tmp_token);

            if(tmp_token_length != 0) {
                tokens[i] = malloc(tmp_token_length);
                strcpy(tokens[i], tmp_token);
            }
            else {
                tokens[i] = NULL;
            }

            i++;
            tmp_token = strtok(input_string, ',');
        }

        // populate your array of arrays of integers
        long** m = malloc(sizeof(long*) * tokens_count);

        for(i=0; i<tokens_count; i++) {
            char* tmp_token = tokens[i];

            if(tmp_token == NULL) {
                m[i] = NULL;
            }
            else {
                m[i] = malloc(sizeof(long));
                m[i][0] = strtol(tmp_token, NULL, 10);
            }
        }
    }
}

但是,您应该使用结构而不是大规模数组来更改数据结构。

答案 2 :(得分:0)

for (j=0;j<(strlen(temp));j++)
{
    for (i=0;i<(strlen(temp));i++)
    {
        if (temp[i]!=(',')&&temp[i]!=(' '))
        {
            m[j][i]=temp[i];
        }   
        else{
            m[j][i]='\0';break;// must end a string with null character.
        }
    }
}

用于打印用途

printf("%s",m[0]);// make a loop for it

答案 3 :(得分:0)

尝试使用scanf获取输入,您的copystring功能似乎很好;但如果有问题,请调试它以查看问题所在。

答案 4 :(得分:-1)

您可以使用fgetsscanf阅读整个字符串,然后使用strtok(string, ",")获取逗号之间的子字符串。

为了检测学生是否错过了一些入学,有很多方法,其中很少有:

1)检查否。在strtok返回NULL之前获得的子字符串 2)您可以在输入字符串中使用,,搜索子串strstr