1
0

answer04.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include <string.h>
  2. #include <ctype.h>
  3. #include <stdio.h>
  4. #include "pa04.h"
  5. #define NUM_CHAR 26
  6. /* 26 English letters */
  7. #define LEN_LINE 80
  8. /* length of a line. Each line has at most 80 characters, including \n
  9. and \0 */
  10. /*
  11. * You are not allowed to use any numeric constant (for example, 65 or
  12. * 97) below. Every constant must be defined as a symbol at the top
  13. * of this file (like NUM_CHAR and LEN_LINE). You will lose 0.2 point
  14. * for each numeric constant below this point.
  15. */
  16. /*
  17. * remember to call fclose if fopen is succeessful.
  18. */
  19. /*
  20. * ============================================================
  21. */
  22. int countLetter(char * file1, char * file2)
  23. /*
  24. * The function counts the occurrences of the twenty sixe English
  25. * characters and write the results to another file.
  26. *
  27. * file1: name of the input file
  28. * file2: name of the output file
  29. *
  30. * All upper-case letters are converted to lower-case letters. 'A' and
  31. * 'a' are both counted as 'a'.
  32. *
  33. * file1: name of the input file
  34. * file2: name of the output file
  35. *
  36. * This function returns EXIT_SUCCESS if it finishes without any
  37. * problem. The function returns EXIT_FAILURE if there is a problem in
  38. * opening the file, reading from the input file, or writing to the
  39. * output file.
  40. */
  41. /*
  42. * You should NOT write anything like the following. You will lose
  43. * 20% points if you do so.
  44. *
  45. * if (c == 'a') ...
  46. * if (c == 'b') ...
  47. * if (c == 'c') ...
  48. * if (c == 'd') ...
  49. *
  50. *
  51. * You should not write anything like the following, either.
  52. * switch (c)
  53. * {
  54. * case 'a':
  55. * ...
  56. * case 'b':
  57. * ...
  58. * case 'c':
  59. * ...
  60. * }
  61. *
  62. * This is error-prone (what happens if you forget to change one
  63. * place?). You should find a general method that is applicable to
  64. * all 26 characters.
  65. *
  66. * Whenever you copy-paste code, you have a higher chance of mistakes.
  67. *
  68. * Hint: create an array to store the counts. Remember to initialize
  69. * the array's elements.
  70. *
  71. * Hint: C has several functions that can be useful. Check isalpha,
  72. * isupper, islower.
  73. *
  74. */
  75. {
  76. return EXIT_SUCCESS;
  77. }
  78. /*
  79. * ============================================================
  80. */
  81. int countString(char * file1, char * file2, char * str)
  82. /*
  83. * The function counts the occurrences of a string in the input file.
  84. * The string is case-sensitive. "ECE" and "ece" are different.
  85. *
  86. * file1: name of the input file
  87. * file2: name of the output file
  88. * str: the string to search
  89. *
  90. * This function returns EXIT_SUCCESS if it finishes without
  91. * problem. The function returns EXIT_FAILURE if there is a problem
  92. * in opening the file, reading from the input file, or writing
  93. * to the output file.
  94. *
  95. * It is possible that the string may appear multiple times in the
  96. * same line. If this occurs, the function should count all all
  97. * occurrences.
  98. *
  99. * You need to count the string within each line. Do not count if the
  100. * string spans two or more lines.
  101. *
  102. * For simplicity, we assume that each line contains at most 80
  103. * characters. Use LEN_LINE and do not use 80. If LEN_LINE is
  104. * defined at the top of this file, you do not need to define anything
  105. * below. You will lose 0.2 point if you use 80 or any number below.
  106. * If you need a number, define a symbol at the top of this file.
  107. *
  108. * Hint: fgets and strstr can be useful.
  109. *
  110. */
  111. {
  112. return EXIT_SUCCESS;
  113. }
  114. /*
  115. * ============================================================
  116. */
  117. int read_front_letter(FILE *fptr, long *index);
  118. int read_back_letter(FILE *fptr, long *index);
  119. /*
  120. * ============================================================
  121. */
  122. int isPalindrome(char * file1, char * file2)
  123. /*
  124. * The function determine whether the input file is a palindrome. All
  125. * upper-case letters are converted to lower-case letters.
  126. *
  127. * file1: name of the input file
  128. * file2: name of the output file
  129. *
  130. * This function returns EXIT_SUCCESS if it finishes without
  131. * problem. The function returns EXIT_FAILURE if there is a problem
  132. * in opening the file, reading from the input file, or writing
  133. * to the output file.
  134. *
  135. * Hint: fseek and ftell can be useful.
  136. *
  137. */
  138. {
  139. /* use ftell to get the size of the file and initialize the
  140. indices */
  141. /* go to the beginning of the file */
  142. /* repeat until the two indices cross over
  143. * read the two alphabet letters from fptr
  144. * the 1st letter is at or after the position pointed at by front_ind
  145. * the 2nd letter is at or before the position pointed at by back_ind
  146. * if the two letters (upper or lower case) are the same, continue the
  147. * while iteration
  148. * else break from the loop
  149. * Note that 'A' and 'a' are treated to be the same letter
  150. */
  151. return EXIT_SUCCESS;
  152. }
  153. int read_front_letter(FILE *fptr, long *ind)
  154. /*
  155. * read an alphabet letter from fptr at the position *index or after
  156. * that you have to skip over non-alphabet letters
  157. *
  158. * if the read is not successful (end of file is reached), the
  159. * function return -1 also, the letter read and returned by this
  160. * function and also the back_letter function should be of the same
  161. * case
  162. *
  163. * update *ind to point to a location right after the read letter
  164. */
  165. {
  166. int c = 0;
  167. return c;
  168. }
  169. /*
  170. * ============================================================
  171. * Do not modify anything below
  172. * ============================================================
  173. */
  174. /*
  175. * read an alphabet letter from fptr at the position *ind or before
  176. * that you have to skip over alphabet letters
  177. *
  178. * if the read is not successful (when *ind is < 0), should return -1
  179. * also, the letter read and returned by this function and also the
  180. * front_letter function should be of the same case
  181. *
  182. * update *ind to point to a location right before the read letter
  183. *
  184. */
  185. int read_back_letter(FILE *fptr, long *ind)
  186. {
  187. while ((*ind) >= 0)
  188. {
  189. fseek(fptr, *ind, SEEK_SET);
  190. int c = fgetc(fptr);
  191. (*ind) -= 1;
  192. if (isalpha(c))
  193. {
  194. if (isupper(c))
  195. {
  196. c = c - 'A' + 'a';
  197. }
  198. return c;
  199. }
  200. }
  201. return -1;
  202. }