[ create a new paste ] login | about

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

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

void convert_to_arr(int x,int y, char ** sv) {

   int i=0,j=0,n=0;
  
   char * nums[x][y];
   char * numstr[(x * y) + 3];
   char tempstr[x + 1];

   for (i=0,n=0; i < x; i++) {
            for (j=0; j < y; j++) {
               if (i== 2 && j == 3) break;
               if ( strcmp( sv[n],"\\n") == 0) {
                    n++;
                    break;
               }
               nums[i][j] = sv[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]);
     }

}

int main()
{
    int xdex,ydex;
    int count = 0, newlines = 0;
     
    char str[] = "1,2,3,\\n,4,5,6,\\n,7,8,9";
    char * save[80];     
    const char s[2] = ",";
    char *token;
 
    printf("Data to parse:\n \"%s\"\n",str);
  
   /* 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);
      
   }

   xdex = (count - newlines) /3;
   ydex = ((count - newlines) /3) + 1;   
   
   convert_to_arr(xdex, ydex, &save);    
   return 0;
}


Output:
1
2
3
4
5
6
7
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: