Question

What I want to do is: The user inputs a string with commas; for example: 123456,44,55,,66

and I want to separate it and store in a new array without the commas; for example:

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

123456 is the student ID number, 44 is the mark for 1st module, 55 is the mark for 2nd module, NULL means that the student didn't take that 3rd module, and 66 is the mark for 4th module.

How can I exactly do that? What I know is that by detecting double commas, it means the student didn't take that 3rd module.

Here is what I have written so far:

#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];
            }   
        }
    }
}
Was it helpful?

Solution

I have edited my code so that you can understand how to declare a function prototype containing 2D array as parameter. Also use fgets() instead of gets(). The function returns the number of marks read , i.e. an integer. I think this might help. Run the code and look on the man pages or google to understand fgets() better.

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

OTHER TIPS

You have to solve your problem in different steps,

The first step is to check how many tokens you have in your input string to be able to allocate enough space to store an array of tokens.

Then you should extract the tokens of the input strings in your tokens string array. To extract the tokens from your input string, you can use the strtok function from <string.h>.

Finally you can use your tokens however you want, like converting them to long in your case.

EDIT: given the requirements, here is a small implementation of what you could do. I don't check the returns of the malloc, you maybe should do it.

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

However, you should probably change your data structure by using structures instead of a massive array.

Try to use scanf for getting input, your copystring function seems fine; but if there is a problem then debug it to see what the problem is.

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.
        }
    }
}

and for priting use

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

You can read entire string using fgets, or scanf and then use strtok(string, ",") to get substrings between commas.

To detect if student has missed some entry, there are many ways, few of them are:

1) Check no. of sub-strings you get before strtok returns NULL.
2) You can search for substring ,, using strstr in the input string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top