Advertisement
vamsiampolu

Split function in C

Feb 7th, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.84 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int mystrlen(char* val)
  5. {
  6.     int i=0;
  7.     while(val[i]!='\0')
  8.     {
  9.         i++;
  10.     }
  11.     return i++;
  12. }
  13.  
  14.     char* substring(char* src,int start,int end)
  15. {
  16.     int i,size_src=mystrlen(src);
  17.     if(start>size_src|| start<0)
  18.         return NULL;
  19.     if(end>size_src)
  20.         end=size_src;
  21.     //['a','b','c','d'] 1,3 ['b','c']
  22.     //starts at start and reads up to but not including end,the extra charecter in calloc is to add a '\0' there
  23.     int size_res=end-start;
  24.     char* res=calloc(size_res+1,sizeof(char));
  25.     for(i=0;i<size_res;i++)
  26.     {
  27.         res[i]=src[start+i];
  28.     }
  29.     res[i]='\0';
  30.     return res;
  31. }
  32.  
  33.     int indexof(char c,char* src)
  34. {
  35.     int i=0;
  36.     int size=mystrlen(src);
  37.     for(i=0;i<size;i++)
  38.     {
  39.         if(c==src[i])
  40.             return i;
  41.     }
  42.     return -1;  
  43. }
  44.  
  45. char* mystrchr(char c,char* src)
  46. {
  47.     int i=0,size=mystrlen(src);
  48.     //must include null charecter
  49.     for(i=0;i<=size;i++)
  50.     {
  51.         if(src[i]==c)
  52.         {
  53.             return src+i;  
  54.         }  
  55.     }
  56.     return NULL;    
  57. }
  58.  
  59. int count(char c,char* src)
  60. {
  61.     int i,size=mystrlen(src);
  62.     int num=0;
  63.     for(i=0;i<size;i++)
  64.     {
  65.       if(c==src[i])
  66.       {
  67.         num++;
  68.       }  
  69.     }
  70.     return num;      
  71. }
  72.  
  73. char** split(char c,char* src)
  74. {
  75.     int i=0,size=mystrlen(src);
  76.     int num=count(c,src);
  77.     int foundAt;
  78.     //For example for the string The0red0ruby0read0to0read,there are five occurences of zero
  79.     //so if you used substring and split you would have ten values,but except for the first and
  80.     //last values,every other value would repeat twice,so remove these from 10...to give you 8/2 non-repeating values
  81.     //the formula: 2+(2n-2)/2 ==>2+n-1 ==>count+1
  82.     num=num+1;
  83.            
  84.     char** res=calloc(num,sizeof(char*));
  85.     while(i<num)
  86.     {
  87.         foundAt=indexof(c,src);
  88.         if(foundAt==0)
  89.         {
  90.             //ignore...you dont need substring here,instead just increment the pointer to start at the next memory location
  91.             src+=1;
  92.         }
  93.         if(foundAt==-1)
  94.         {
  95.             res[i]=src;
  96.             break;
  97.         }
  98.  
  99.         if(foundAt>=size)
  100.         {
  101.             //ignore if found at the last charecter or null terminator
  102.             break;
  103.         }
  104.         //you need to use substring here because you need a beginning and an end
  105.         res[i]=substring(src,0,foundAt);
  106.         //pointer arithmetic...
  107.         src=src+foundAt+1;
  108.         if(src==NULL)
  109.         {
  110.             break;
  111.         }  
  112.         i++;
  113.     }  
  114.     return res;
  115. }
  116.  
  117. int main(int argc,char** argv)
  118. {
  119.     char* val="The0Red0Riding0Hood0winked0at0the0big0bad0wolf";
  120.     char** res=split('0',val);
  121.     while(*res)
  122.     {
  123.         printf("%s\n",*res);
  124.         res++;
  125.     }
  126.     //free(res);
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement