Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. // Program to find one string in another, delete the found string, and replace it with another
  2.  
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5.  
  6. int main (void)
  7. {
  8. char source[81], s1[81], s2[81];
  9. void readLine (char buffer[]);
  10. void replaceString (char source[], char s1[], char s2[]);
  11. void printString (char source[]);
  12.  
  13. // Enter source
  14. printf ("Enter the source string:n");
  15. readLine (source);
  16.  
  17. // Enter string to be replaced
  18. printf ("Enter the string to be replaced:n");
  19. readLine (s1);
  20.  
  21. // Enter string that will replace
  22. printf ("Enter the string that will replace the previous one:n");
  23. readLine (s2);
  24.  
  25. // Replace the strings
  26. replaceString (source, s1, s2);
  27.  
  28. // Print the string
  29. printString (source);
  30.  
  31. return 0;
  32. }
  33.  
  34. void readLine (char buffer[])
  35. {
  36. char character;
  37. int i = 0;
  38.  
  39. do
  40. {
  41. character = getchar ();
  42. buffer[i] = character;
  43. i++;
  44. }
  45. while ( character != 'n' );
  46.  
  47. buffer[i - 1] = '';
  48. }
  49.  
  50. bool equalCharacter (char a, char b)
  51. {
  52. if ( a == b )
  53. return true;
  54.  
  55. else
  56. return false;
  57.  
  58. }
  59.  
  60. void replaceString (char source[], char s1[], char s2[])
  61. {
  62. int start, numberRemove;
  63. int findString (char string[], char find[]);
  64. int stringLength (char string[]);
  65. void removeString (char source[], int start, int numberRemove);
  66. void inString (char source[], char insertString[], int position);
  67.  
  68.  
  69. start = findString (source, s1);
  70. numberRemove = stringLength (s1);
  71. removeString (source, start, numberRemove);
  72.  
  73. inString (source, s2, start);
  74.  
  75. }
  76.  
  77.  
  78. int findString (char string[], char find[])
  79. {
  80. int i = 0, j = 0, start;
  81.  
  82. bool equalCharacter (char a, char b);
  83.  
  84. for ( i = 0; string[i] != '' && find[j] != ''; i++ ) {
  85.  
  86. if ( equalCharacter (string[i], find[j]) )
  87. j++;
  88.  
  89. else
  90. j = 0;
  91. }
  92.  
  93. if ( j == 0 )
  94. start = -1;
  95.  
  96. else
  97. start = i - j;
  98.  
  99.  
  100. return start;
  101.  
  102. }
  103.  
  104. int stringLength (char string[])
  105. {
  106. int i, length = 0;
  107.  
  108. for (i = 0; string[i] != ''; i++) {
  109. length++;
  110. }
  111.  
  112. return length;
  113. }
  114.  
  115. void removeString (char source[], int start, int numberRemove)
  116. {
  117. int i, j;
  118.  
  119. for ( i = start, j = start + numberRemove; i <= start + numberRemove - 1; j++, i++ ){
  120. source[i] = source[j];
  121. }
  122.  
  123.  
  124. source[i] = '';
  125.  
  126.  
  127. }
  128.  
  129. void inString (char source[], char insertString[], int position)
  130. {
  131. int i, j;
  132. char buffer[81];
  133.  
  134. for ( i = position, j = 0; source[i] != ''; i++, j++ ) {
  135. buffer[j] = source[i];
  136. }
  137.  
  138. buffer[j] = '';
  139.  
  140.  
  141. for ( i = position, j = 0; insertString[j] != ''; i++, j++ ) {
  142. source[i] = insertString[j];
  143. }
  144.  
  145.  
  146. for ( j = 0; buffer[j] != ''; i++, j++ ) {
  147. source[i] = buffer[j];
  148.  
  149.  
  150.  
  151.  
  152. source[i] = '';
  153.  
  154. }
  155.  
  156. void printString (char source[])
  157. {
  158. printf ("%s", source);
  159. printf ("n");
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement