i am trying to write a split function ...which will accept a array and split it down to diffrent strings on the basis of '-' symbol

can u tell me where i am wrong

i am getting segmentation fault ...i think it is due to the pointer array i used ..

here is code

#include<stdlib.h>
#include<stdio.h>


int split(char spt[])
  {
   char *t;
   int count,i;
   t=&spt[0];
    
  count=0;
   while(*t!='\0')
   {
    if(*t=='-')
     {
       count=count+1;
       }
      t++;
    }


char *ptr[count+1];

count=0;
t=&spt[0];
i=0;
while(*t!='\0')
  {
    while(*t!='-')
     {
     ptr[count][i]=*t;
      i++;
       }

     if(*t=='-')
      {
       count=count+1;
       i=0;
       }
  t++;
}

printf("the  first value is %s \n ",ptr[0]);
printf("the  second   value is %s \n",ptr[1]);

return   0;S
}

int main(int argc ,char *argv[])
 {
   char check[]="HEllo-le-t-me-confirm-it";
   split(check);

return 0;
}

Not sure what you wanted to do at this point, but you can't do this in C (it can compile but I'll give you segmentation faults just as you reported):

char *ptr[count+1];

You must decide explicitly what size you wanted ptr to have. If you don't know for sure, you'll need to perform dynamic allocation instead (using malloc());

Your code has too many problems for Ed to cover in detail right now, so here is a simple function that works using the library functions strcspn, strspn, and printf for any heavy lifting. If you cannot use strcspn or strspn, the problem can be broken down into the tasks they perform:

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

void split(char const *s)
{
    while (*s)
    {
        size_t last = strcspn(s, "-");     // Find the end of the token
        printf("Found '%.*s'\n", last, s);
        s += last;                         // Skip the token
        s += strspn(s, "-");               // Skip adjacent delimiters
    }
}

int main()
{
    split("HEllo-le-t-me-confirm-it");
}

The simplest way to write that program would be to use strtok() function. But if you are not permitted to use that, then you will just have to write your own.


>>char *ptr[count+1];

C99 will not allow you to do that, but vuture versions of C may. For now you need to call malloc() to allocate count+1 bytes: char* ptr = malloc(count+1); >> ptr[count]=*t;
ptr is just a one-dimensional array of characters. That line is treating it as a two dimensional array. If that's what you want to do then you have to redefine ptr like this: char **ptr = malloc((count+1) * sizeof(char*)); Then you will have to allocate space for each line in the array. Before you can copy characters into the array you have to know how many characters are going to be copied so that you can allocate the appropriate amount of memory.

Your split function has to be made a little smarter. First find out how many characters there are in the current segment, malloc() space to hold that many characters, then finally copy the characters. After that go on to the next segment and do the same thing again.

[edit]Didn't see Edward's ^^^^ post, but he has presented another way to accomplish that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.