[ create a new paste ] login | about

Link: http://codepad.org/KOVKzmtv    [ raw code | output | fork ]

slevy1ster - C, pasted on Nov 8:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int i,j,n;
    int count = 0, newlines = 0;
     
    char str[] = "1,2,3,\\n,4,5,6,\\n,7,8,9";
    char * save[80];
    char tsave[2];
    printf("Data to parse: \"%s\"\n",str);
    
    const char s[2] = ",";
    char *token;
   
   /* setup */
   token = strtok(str, s);
   
   /* get tokens */
   while( token != NULL ) 
   {
      
      save[count] = token;
      if (strcmp(save[count],"\\n") == 0) { newlines++; }
      count++; 
      token = strtok(NULL, s);
      
   }
   /*printf("Number of tokens is: %d\n",count);*/
   /*printf("numbers are %d\n",count - newlines);*/
   int xdex = (count - newlines) /3;
   int ydex = ((count - newlines) /3) + 1;   
   char * nums[xdex][ydex];
   char * numstr[(xdex * ydex) + 3];
   char tempstr[xdex + 1];
    for (i=0,n=0; i < xdex; i++) {
            for (j=0; j < ydex; j++) {
               if (i== 2 && j == 3) break;
               if ( strcmp( save[n],"\\n") == 0) {
                    n++;
                    break;
               }
               nums[i][j] = save[n++];
            }
    }
puts("\nOutputting array of three strings and 3 chars in ea:");    
    for (i=0; i < 3; i++) { 
         strcpy( tempstr, nums[i][0] );
         strcat( tempstr,nums[i][1] );
         strcat( tempstr,nums[i][2] );
         numstr[i] = tempstr;
         printf("%s\n",numstr[i]);
     }
    
    return 0;
}


Output:
1
2
3
4
5
6
Data to parse: "1,2,3,\n,4,5,6,\n,7,8,9"

Outputting array of three strings and 3 chars in ea:
123
456
789


Create a new paste based on this one


Comments: