C 如何分离阵列并将其存储在新阵列中?

C 如何分离阵列并将其存储在新阵列中?,c,arrays,string,strcpy,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表示学生没有参加第三个模块,66是第四个模块的标记 我该怎么做呢?我所知道的是,通过检测双逗号,这意味着该学生没有学习第三个模块 以下是我到目前为止所写

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

我想把它分开,存储在一个新的数组中,不带逗号;例如:

m[0][]={123456}, m[1][]={44}, m[2][]={55}, m[3][]={}, m[4][]={66}
123456
是学生ID号,
44
是第一个模块的标记,
55
是第二个模块的标记,
NULL
表示学生没有参加第三个模块,
66
是第四个模块的标记

我该怎么做呢?我所知道的是,通过检测双逗号,这意味着该学生没有学习第三个模块

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

#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];
            }   
        }
    }
}
#包括
#包括
#包括
无效复制字符串(字符m[],字符温度[]);
main()
{
煤焦温度[10000];
字符m[10000][10000];
获取(临时);
复制字符串(m,temp);
printf(“%s\n”,m);
返回0;
}
无效复制字符串(字符m[],字符温度[])
{
int i;
int j;

对于(j=0;j您可以使用
fgets
scanf
读取整个字符串,然后使用
strtok(string,“,”)
获取逗号之间的子字符串

要检测学生是否遗漏了一些条目,有很多方法,其中很少有:

1) 检查在
strtok
返回
NULL
之前得到的子字符串的数量

2) 您可以使用输入字符串中的
strstr
搜索子字符串

尝试使用
scanf
获取输入,您的
copystring
函数看起来不错;但如果有问题,则调试它以查看问题所在。

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

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.
        }
    }
}
#包括
#包括
#包括
#定义大小1000
int stringcopy(字符m[][大小],字符温度[]);
main()
{
字符温度[SIZE],m[100][SIZE];
FGET(温度、尺寸、标准偏差);
int num=stringcopy(m,temp);
int i;

对于(i=0;i你必须用不同的步骤解决你的问题

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

然后,您应该提取令牌字符串数组中输入字符串的令牌。要从输入字符串中提取令牌,可以使用
中的
strtok
函数

最后,您可以随心所欲地使用您的代币,比如在您的案例中将它们转换为
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);
            }
        }
    }
}
int main(int argc,char**argv){
int i;
char*input_string=/*某处的输入字符串*/;
字符**代币;
int tokens_count=0;
char*input_ptr=输入_字符串;
char*tmp_令牌;
大小\u t tmp\u标记\u长度;
//计算您的分离器的出现次数以获得元素的数量
对于(;input_ptr[tokens_count];input_ptr[tokens_count]==','?tokens_count++:input_ptr++);
如果(令牌数=0){
//找不到代币,你想在这里做什么?
}
否则{
//构建我们的令牌数组
代币=malloc(sizeof(*代币)*代币数量);
i=0;
tmp_标记=strtok(输入_字符串,',');
while(tmp_令牌!=NULL){
tmp_令牌长度=strlen(tmp_令牌);
如果(tmp_令牌长度!=0){
令牌[i]=malloc(tmp_令牌长度);
strcpy(令牌[i],tmp_令牌);
}
否则{
令牌[i]=NULL;
}
i++;
tmp_标记=strtok(输入_字符串,',');
}
//填充整数数组的数组
long**m=malloc(sizeof(long*)*tokens\u计数);

对于(i=0;i调用
copystring(char**,char*))
未定义。您卡在哪里?您缺少什么?请具体说明?不要使用。Smac89,抱歉,但我真的不知道如何正确定义。NiRR,抱歉,但我不确定我的代码中是否存在问题,我不知道如何更正。您能帮我检查一下吗?moeCake,我应该使用什么?向用户w何否决:我在我的答案中添加了更多内容,请检查一下。你能给我看一下完整的代码吗?谢谢。我真的不知道如何使用strtok,strstr并不是那么难,请参考,这里你也可以找到
strstr
的帮助。如果你不知道它们的用途,给你完整的代码将不会有帮助。我想你会的是的,这些函数将字符串转换为整数,casting是一个术语,用于表示通过使用cast运算符进行的数据类型更改。这很挑剔,但确实是转换。它不起作用。=(我想让它像m[0][={123456},m[1][={44},m[2][={55},m[3][=},m[4][]={66}。我运行了上面的代码,得到了以下输出:123456 44 55 66这意味着m[0]={123456},m[1]={44},m[2]={55}和m[3]={}和m[4]={66}。这不足以解决你的问题吗?我得到了这个警告:指向不完整类型m[j][k++]=temp[I]的指针上的算术实际上在你的主函数中,你正在传递m(一个2d数组)复制字符串(需要1d数组),因为您定义的原型是错误的。只需复制并粘贴我的整个程序并运行它。然后您就会理解代码中的问题。您能教我如何正确定义原型吗?我运行您的程序,最后一个for循环只打印出m[0],如果逐个打印,它就会工作。printf(“%s\n”,m[0]);printf(“%s\n”,m[1]);等等…在函数开头放置一个断点,然后在调试模式下运行。您使用哪个编译器?
#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;
}
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);
            }
        }
    }
}