pa05.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Do not modify this file.
  3. */
  4. #include "pa05.h"
  5. #define LINE_SIZE 1000
  6. void trim(char *s)
  7. {
  8. int pos = strlen(s) - 1;
  9. while (iscntrl(s[pos])) {
  10. s[pos--] = '\0';
  11. }
  12. }
  13. int main(int argc, char *argv[])
  14. {
  15. if (argc != 4) {
  16. printf("usage: %s <command> <inputfile> <outputfile>\n", argv[0]);
  17. return EXIT_FAILURE;
  18. }
  19. FILE *f;
  20. if ((f = fopen(argv[2], "r")) == NULL) {
  21. printf("unable to open file %s!\n", argv[2]);
  22. return EXIT_FAILURE;
  23. }
  24. FILE *fptrout;
  25. if ((fptrout = fopen(argv[3], "w")) == NULL) {
  26. printf("unable to open file %s!\n", argv[3]);
  27. fclose(f);
  28. return EXIT_FAILURE;
  29. }
  30. int num_lines = 0;
  31. char buffer[LINE_SIZE];
  32. while (fgets(buffer, LINE_SIZE, f) != NULL) {
  33. num_lines++;
  34. }
  35. rewind(f);
  36. char **src = malloc(sizeof(char *) * num_lines);
  37. char **copy = malloc(sizeof(char *) * num_lines);
  38. int i;
  39. for (i = 0; i < num_lines; i++) {
  40. if (feof(f)) {
  41. printf("not enough num_lines in file!\n");
  42. fclose(f);
  43. fclose(fptrout);
  44. return EXIT_FAILURE;
  45. }
  46. copy[i] = malloc(sizeof(char) * LINE_SIZE);
  47. fgets(copy[i], LINE_SIZE, f);
  48. trim(copy[i]);
  49. src[i] = strdup(copy[i]);
  50. }
  51. fclose(f);
  52. int total_length = 0;
  53. for (i = 0; i < num_lines; i++) {
  54. total_length += my_strlen(copy[i]);
  55. }
  56. char *buf = malloc(sizeof(char) * total_length + num_lines + 1);
  57. buf[0] = '\0';
  58. //Partitioning outputs
  59. if (strcmp(argv[1], "my_strlen") == 0) {
  60. for (i = 0; i < num_lines; i++) {
  61. fprintf(fptrout, "length: %d\n", my_strlen(copy[i]));
  62. }
  63. } else if (strcmp(argv[1], "my_countchar") == 0) {
  64. for (i = 0; i < num_lines; i++) {
  65. fprintf(fptrout, "count(%c): %d\n", copy[i][0], my_countchar(copy[i], copy[i][0]));
  66. }
  67. } else if (strcmp(argv[1], "my_strupper") == 0) {
  68. for (i = 0; i < num_lines; i++) {
  69. my_strupper(copy[i]);
  70. fprintf(fptrout, "uppercase: %s\n", copy[i]);
  71. }
  72. } else if (strcmp(argv[1], "my_strlower") == 0) {
  73. for (i = 0; i < num_lines; i++) {
  74. my_strlower(copy[i]);
  75. fprintf(fptrout, "lowercase: %s\n", copy[i]);
  76. }
  77. } else if (strcmp(argv[1], "my_strcat") == 0) {
  78. for (i = 0; i < num_lines; i++) {
  79. my_strcat(copy[i], " ");
  80. my_strcat(copy[i], src[i]);
  81. fprintf(fptrout, "%s\n", copy[i]);
  82. }
  83. } else if (strcmp(argv[1], "my_strncat") == 0) {
  84. for (i = 0; i < num_lines; i++) {
  85. my_strncat(copy[i], " ", 2);
  86. my_strncat(copy[i], "........", 1);
  87. fprintf(fptrout, "%s\n", copy[i]);
  88. }
  89. } else if (strcmp(argv[1], "my_strcpy") == 0) {
  90. for (i = 0; i < num_lines; i++) {
  91. my_strcpy(copy[i], "Copying this String.");
  92. fprintf(fptrout, "%s\n", copy[i]);
  93. }
  94. } else if (strcmp(argv[1], "my_strncpy") == 0) {
  95. for (i = 0; i < num_lines; i++) {
  96. my_strncpy(copy[i], src[i], 5);
  97. fprintf(fptrout, "%s\n", copy[i]);
  98. }
  99. } else if (strcmp(argv[1], "my_strstr") == 0) {
  100. for (i = 0; i < num_lines; i++) {
  101. fprintf(fptrout, "%d\n", my_strstr(copy[i], src[i]) != NULL);
  102. }
  103. } else if (strcmp(argv[1], "my_strinsert") == 0) {
  104. for (i = 0; i < num_lines; i++) {
  105. my_strinsert(buf, "\n", 0);
  106. my_strinsert(buf, src[i], 0);
  107. }
  108. fprintf(fptrout, "[%s]\n", buf);
  109. } else if (strcmp(argv[1], "my_strdelete") == 0) {
  110. for (i = 0; i < num_lines; i++) {
  111. my_strinsert(buf, "\n", 0);
  112. my_strinsert(buf, src[i], 0);
  113. my_strdelete(buf, 0, 10);
  114. }
  115. fprintf(fptrout, "[%s]\n", buf);
  116. }
  117. for (i = 0; i < num_lines; i++) {
  118. free(copy[i]);
  119. free(src[i]);
  120. }
  121. free(src);
  122. free(copy);
  123. free(buf);
  124. fclose(fptrout);
  125. return EXIT_SUCCESS;
  126. }