删除子串.c 788 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include <stdio.h>
  2. #include <string.h>
  3. int main() {
  4. char str[100], sub[100];
  5. fgets(str, 100, stdin);
  6. fgets(sub, 100, stdin);
  7. // Remove newline characters
  8. str[strcspn(str, "\n")] = '\0';
  9. sub[strcspn(sub, "\n")] = '\0';
  10. int str_len = strlen(str);
  11. int sub_len = strlen(sub);
  12. char result[100];
  13. int result_index = 0;
  14. int i,j;
  15. for (i = 0; i < str_len; i++) {
  16. int match = 1;
  17. for (j = 0; j < sub_len; j++) {
  18. if (str[i + j] != sub[j]) {
  19. match = 0;
  20. break;
  21. }
  22. }
  23. if (match) {
  24. i += sub_len - 1;
  25. } else {
  26. result[result_index++] = str[i];
  27. }
  28. }
  29. result[result_index] = '\0';
  30. printf("%s\n", result);
  31. return 0;
  32. }