Advertisement
Guest User

Untitled

a guest
May 30th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <String.h>
  4.  
  5. /*
  6. * === FUNCTION ======================================================================
  7. * Name: del_str
  8. * Description: Delete substring in the source string
  9. * =====================================================================================
  10. */
  11. int
  12. del_str (char str[], char const *substr )
  13. {
  14. int index_start = 0;
  15. int index_end = 0;
  16. char *iterator;
  17.  
  18. // Find index of first char
  19. for (iterator = str; *iterator != *substr; iterator++) {
  20. index_start++;
  21. }
  22.  
  23. for (index_end = index_start; *substr != '\0'; substr++) {
  24. if (*iterator++ != *substr) {
  25. break;
  26. }
  27.  
  28. index_end++;
  29. }
  30.  
  31. if (*substr != '\0') return 0;
  32.  
  33. int length = strlen(str);
  34.  
  35. // Add characters from index_end to end of str
  36. for (int i = index_end; i < length; i++) {
  37. str[index_start++] = *(str + index_end++);
  38. }
  39.  
  40. str[index_start] = '\0';
  41.  
  42. // Match rest of characters
  43. return 1;
  44. } /* ----- end of function del_str ----- */
  45.  
  46.  
  47. /*
  48. * === FUNCTION ======================================================================
  49. * Name: main
  50. * Description:
  51. * =====================================================================================
  52. */
  53. int
  54. main ( int argc, char *argv[] )
  55. {
  56. char string1[] = "ABCDEF GXYZ";
  57. char *sub_str = "DEF";
  58.  
  59. int final = del_str(string1, sub_str);
  60. printf("Result: %d\nFinal String: %s\n", final, string1);
  61.  
  62. return EXIT_SUCCESS;
  63. } /* ---------- end of function main ---------- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement