fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. void split(char *ch,char **part1,char **part2,int *num){
  6. int length=strlen(ch);
  7. char *lc1,*lc2;
  8. int i=0,j=0;
  9. printf("value of length is %d\n",length);
  10.  
  11. printf("this function recieved %s for splitting into pieces\n",ch);
  12.  
  13. lc1=(char *)malloc(length*sizeof(char));
  14. lc2=(char *)malloc(length*sizeof(char));
  15.  
  16. while(ch[i]!=' '){
  17. lc1[i]=ch[i];
  18. printf("\nin loop with lc1[i] = %c and ch[i] = %c",lc1[i],ch[i]);
  19. i++;
  20. }
  21. lc1[i]='\0';
  22. i++;
  23. while(ch[i]!=' '){
  24. lc2[j]=ch[i];
  25. printf("\nin loop with lc2[j] = %c and ch[i] = %c",lc2[j],ch[i]);
  26. j++;
  27. i++;
  28. }
  29. lc2[j]='\0';
  30. i++;
  31. *num=atoi(&ch[i]);
  32.  
  33. *part1=lc1;
  34. *part2=lc2;
  35.  
  36. printf("\nsplit results are:\n");
  37. printf("part1=%s and part2=%s and num=%d and lc1=%s lc2=%s //this is surprising me\n",*part1,*part2,*num,lc1,lc2);
  38.  
  39. }
  40.  
  41. int main()
  42. {
  43. int N,i,j,n,*numArray,count=0;
  44. char **arr,*part1,*part2,*token;
  45. scanf("%d",&N);
  46. arr=malloc(N*sizeof *arr);
  47. numArray=malloc(N*sizeof *numArray);
  48. for(i=0;i<N;i++){
  49. arr[i]=(char *)malloc(50*sizeof(char));
  50. }
  51.  
  52. for(i=0;i<N;i++){
  53. printf("plz enter %d th :",i);
  54. scanf(" ");
  55. gets(&arr[i][0]);
  56. }
  57.  
  58. for(i=0;i<N;i++){
  59. printf("%s\n",arr[i]);
  60. }
  61.  
  62. for(i=0;i<N;i++){
  63. /*token=strtok(arr[i]," ");
  64.   part1=token;
  65.   token=strtok(NULL," ");
  66.   part2=token;
  67.   token=strtok(NULL," ");
  68.   n=atoi(token);*/
  69. split(arr[i],&part1,&part2,&n);
  70. //some logic to use part1 and part2 of the sentence
  71. }
  72. return 0;
  73.  
  74. }
  75.  
Success #stdin #stdout 0s 2296KB
stdin
1
abcd efgh 2
stdout
plz enter 0 th :abcd efgh 2
value of length is 11
this function recieved abcd efgh 2 for splitting into pieces

in loop with lc1[i] = a and ch[i] =  a
in loop with lc1[i] = b and ch[i] =  b
in loop with lc1[i] = c and ch[i] =  c
in loop with lc1[i] = d and ch[i] =  d
in loop with lc2[j] = e and ch[i] =  e
in loop with lc2[j] = f and ch[i] =  f
in loop with lc2[j] = g and ch[i] =  g
in loop with lc2[j] = h and ch[i] =  h
split results are:
part1=abcd and part2=efgh and num=2 and lc1=abcd lc2=efgh    //this is surprising me