fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. int main(int argc, const char * argv[]) {
  7. // insert code here...
  8. FILE *fp;
  9. int width=20,height=50;
  10. char array[height];
  11. char words [width][height];
  12. memset(&words, 0, sizeof words); // good practice
  13. int counter=0; // To traverse through array and tracks the current position in array.
  14.  
  15. //fp = fopen("data.txt", "r");
  16. fp = stdin;
  17. if (fp == NULL) {
  18. perror("fopen");
  19. exit(EXIT_FAILURE);
  20. }
  21.  
  22. int i; for(i=0;i<width;i++)
  23. {
  24. if ( counter == 0 )
  25. if ( !fgets(array, sizeof array, fp) )
  26. break;
  27.  
  28. for(int j=0;j<height;j++)
  29. {
  30. if(array[counter]==','||array[counter]=='\0')
  31. {
  32. words[i][j]=0;
  33. //printf("words[%d][%d] = 0\n", i, j);
  34. if ( array[counter] == '\0' )
  35. {
  36. //printf("Next line.\n");
  37. counter = 0;
  38. }
  39. else
  40. ++counter;
  41.  
  42. break;
  43. }
  44. else
  45. {
  46. words[i][j]=array[counter++];
  47. //printf("words[%d][%d] = %c\n", i, j, isprint(array[counter]) ? array[counter] : '?');
  48. }
  49. }
  50. }
  51.  
  52. printf("Total words: %d\n", i);
  53. for (int n=0; n < i; n++) {
  54. printf("%s\n", words[n]);
  55. }
  56.  
  57. //fclose(fp);
  58.  
  59. return 0;
  60. }
  61.  
Success #stdin #stdout 0s 2116KB
stdin
Hello,My,name,is,Ram.
I,own,20,thousand,bucks.
stdout
Total words: 10
Hello
My
name
is
Ram.

I
own
20
thousand
bucks.