Guest User

Untitled

a guest
Apr 24th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #define MAX_SIZE 100
  3.  
  4. // 찾는 함수
  5. int search(char *sentence, int sentence_length, char *ex_word, int ex_word_length)
  6. {
  7. int sentence_index = 0;
  8. int ex_word_index = 0;
  9. int index = 0;
  10. int j = 0;
  11. while (sentence_index <= sentence_length)
  12. {
  13. if (sentence[sentence_index] == ex_word[ex_word_index])
  14. {
  15. sentence_index++;
  16. ex_word_index++;
  17. int match_count = 1;
  18.  
  19. while(ex_word_index < ex_word_length)
  20. {
  21. if (sentence[sentence_index] != ex_word[ex_word_index])
  22. {
  23. break;
  24. }
  25. else
  26. {
  27. match_count++;
  28. sentence_index++;
  29. ex_word_index++;
  30. }
  31. }
  32.  
  33. if (match_count == ex_word_length)
  34. {
  35. return sentence_index - ex_word_length;
  36. }
  37. else
  38. {
  39. ex_word_index = 0;
  40. }
  41. }
  42. else
  43. {
  44. sentence_index++;
  45. ex_word_index = 0;
  46. }
  47. }
  48.  
  49. return -1; // 이 index는 삭제위치
  50. }
  51.  
  52. // 삭제 함수
  53. void delete (char *sentence, int sentence_index, int word_length)
  54. {
  55. int index;
  56. for (index = sentence_index; index < sentence_index + word_length; index++)
  57. {
  58. sentence[index] = ' ';
  59. }
  60. }
  61.  
  62. // 이동 함수
  63. // sentence_index 에서 부터 amount 만큼 sentence 를 밀어준다
  64. void shift(int ex_word_length, int amount, char *sentence, int sentence_index, int sentence_length)
  65. {
  66. int index;
  67. // ex_word_length : length of existing word
  68. // amount : difference
  69. // sentence_index : index of ex_word in sentence
  70.  
  71. if(amount == 0)
  72. {
  73. return;
  74. }
  75.  
  76. if (amount > 0)
  77. {
  78. for(index = sentence_index + ex_word_length; index <= sentence_length + amount; index++)
  79. {
  80. sentence[index - amount] = sentence[index];
  81. }
  82. return;
  83. }
  84.  
  85. if (amount < 0)
  86. {
  87. amount *= -1;
  88. for(index = sentence_length + amount; index >= sentence_index + ex_word_length; index--)
  89. {
  90. sentence[index + amount] = sentence[index];
  91. }
  92. return;
  93. }
  94. }
  95.  
  96. // 삽입 함수
  97. void insert(char *sentence, int index, char *new_word, int new_word_length)
  98. {
  99. int i;
  100. for (i = 0; i < new_word_length; i++)
  101. {
  102. sentence[index] = new_word[i];
  103. index++;
  104. }
  105. }
  106.  
  107. // 문자길이 찾기
  108. int find_word_length(char *array)
  109. {
  110. int i = 0;
  111. while (1)
  112. {
  113. if (array[i] == '\n' || array[i] == '\0')
  114. {
  115. array[i] = '\0';
  116. return i;
  117. }
  118. i++;
  119. }
  120. }
  121.  
  122. int main(void)
  123. {
  124. int index; // 문장에서 단어가 나오는 자리
  125. int i = 0;
  126. int j;
  127. int difference; // ex_word 길이 - new_word 길이
  128. char sentence[1000] = { '\0', }; // 문장
  129. char ex_word[MAX_SIZE] = { '\0', }; // 찾는 단어
  130. char new_word[MAX_SIZE] = { '\0', }; // 바꿀 단어
  131.  
  132. int sentence_length;
  133. int ex_word_length;
  134. int new_word_length;
  135.  
  136. fgets(sentence, 1000, stdin);
  137. sentence_length = find_word_length(sentence);
  138.  
  139. while (1)
  140. {
  141. i = 0;
  142. sentence_length = find_word_length(sentence);
  143.  
  144. fgets(ex_word, sizeof(ex_word),stdin);
  145. if (ex_word[0] == '0')
  146. {
  147. break;
  148. }
  149. fgets(new_word, sizeof(new_word), stdin);
  150. if (new_word[0] == '0')
  151. {
  152. break;
  153. }
  154. ex_word_length = find_word_length(ex_word);
  155. new_word_length = find_word_length(new_word);
  156.  
  157. difference = ex_word_length - new_word_length;
  158.  
  159. while(1)
  160. {
  161. index = search(sentence + i, sentence_length - i, ex_word, ex_word_length);
  162. if (index == -1)
  163. {
  164. break;
  165. }
  166. index += i;
  167.  
  168. delete(sentence, index, ex_word_length);
  169. shift(ex_word_length, difference, sentence, index, sentence_length);
  170. insert(sentence, index, new_word, new_word_length);
  171.  
  172. i = index + new_word_length;
  173. }
  174.  
  175. printf("%s\n", sentence);
  176.  
  177. for (j = 0; j < ex_word_length; j++)
  178. {
  179. ex_word[j] = '\0';
  180. }
  181. for (j = 0; j < new_word_length; j++)
  182. {
  183. new_word[j] = '\0';
  184. }
  185.  
  186. }
  187. return 0;
  188. }
Add Comment
Please, Sign In to add comment