Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <String.h>
- /*
- * === FUNCTION ======================================================================
- * Name: del_str
- * Description: Delete substring in the source string
- * =====================================================================================
- */
- int
- del_str (char str[], char const *substr )
- {
- int index_start = 0;
- int index_end = 0;
- char *iterator;
- // Find index of first char
- for (iterator = str; *iterator != *substr; iterator++) {
- index_start++;
- }
- for (index_end = index_start; *substr != '\0'; substr++) {
- if (*iterator++ != *substr) {
- break;
- }
- index_end++;
- }
- if (*substr != '\0') return 0;
- int length = strlen(str);
- // Add characters from index_end to end of str
- for (int i = index_end; i < length; i++) {
- str[index_start++] = *(str + index_end++);
- }
- str[index_start] = '\0';
- // Match rest of characters
- return 1;
- } /* ----- end of function del_str ----- */
- /*
- * === FUNCTION ======================================================================
- * Name: main
- * Description:
- * =====================================================================================
- */
- int
- main ( int argc, char *argv[] )
- {
- char string1[] = "ABCDEF GXYZ";
- char *sub_str = "DEF";
- int final = del_str(string1, sub_str);
- printf("Result: %d\nFinal String: %s\n", final, string1);
- return EXIT_SUCCESS;
- } /* ---------- end of function main ---------- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement