#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int erase_once(char *input_string, const char *substring, int case_sensitive) {
size_t string_length = strlen(input_string);
size_t substring_length = strlen(substring);
if (string_length < substring_length || substring_length == 0) return 0;
size_t i;
for (i = 0; i < string_length - substring_length + 1; i++) {
int match_found = 1;
for (size_t j = i; j < i + substring_length; j++) {
if ((case_sensitive == 0 &&
tolower(input_string[j]) != tolower(substring[j-i])) ||
input_string[j] != substring[j-i]) {
match_found = 0;
break;
}
}
if (!match_found) continue;
for (; i < string_length - substring_length; i++) {
input_string[i] = input_string[i + substring_length];
}
input_string[i] = '\0';
return 1;
}
return 0;
}
int erase(char *input_string, const char *substring, int case_sensitive) {
int count;
for (count = 0; erase_once(input_string, substring, case_sensitive); count++);
return count;
}
int main(void) {
char main_string[] = "abcdef cde AbCDE";
const char *substring_to_erase = "abc";
erase(main_string, substring_to_erase, 0);
printf("%s\n", main_string);
return 0;
}
I tryed to make it case sensitive by putting the variable int case_sensitive; and when that wariable is 0 it is not sensitive and when it is 1 it is case sensitive so for example: for input "abcdef cde AbCDE" and substring "abc" if it is 1 it should return -"def cde AbCDE"; and for the same input and case_sensitive set to 0 it should return "def cde DE" another example is for string xYXyXYXY and substring xy and sensitive set to 0 it should return an empty string
but it doesnt, can you help me find the mistake?
Split the task into smaller bits
https://godbolt.org/z/M7bh8Pq7a