Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

23%
+1 −8
Q&A How to efficiently remove every occurrence of one string inside another

I have two strings and want to remove every occurrence of the second string inside the first one starting from the beginning of the first string and as if each occurrence was removed immediately. ...

1 answer  ·  posted 3y ago by dumplings‭  ·  edited 2y ago by __blackjack__‭

#8: Post edited by user avatar __blackjack__‭ · 2022-07-13T07:55:03Z (over 2 years ago)
Restore the tags of the original question.
How to efficiently remove every occurrence of one string inside another
I have two strings and want to remove every occurrence of the second string inside the first one starting from the beginning of the first string and as if each occurrence was removed immediately.
I would like to do so in C in a time-efficient way, i.e. probably with the lowest number of comparisons and include/copy actions.
#7: Post edited by user avatar Alexei‭ · 2022-06-26T16:32:58Z (over 2 years ago)
rollback to the original content
  • Haskell Question
  • How to efficiently remove every occurrence of one string inside another
  • > Write a program that takes in three integers which represent the three sides of a triangle and returns appropriate Boolean values using 'strtok'. The program should ask to enter the 3 sides and deduce (True/False) whether the triangle is Isosceles, Equilateral...
  • I have two strings and want to remove every occurrence of the second string inside the first one starting from the beginning of the first string and as if each occurrence was removed immediately.
  • I would like to do so in C in a time-efficient way, i.e. probably with the lowest number of comparisons and include/copy actions.
#6: Post edited by user avatar dumplings‭ · 2022-06-26T13:58:23Z (over 2 years ago)
  • How to efficiently remove every occurrence of one string inside another
  • Haskell Question
  • I have two strings and want to remove every occurrence of the second string inside the first one starting from the beginning of the first string and as if each occurrence was removed immediately.
  • I would like to to so in C in a time efficient way, i.e. probably with the lowest number of comparisons and include/copy actions.
  • > Write a program that takes in three integers which represent the three sides of a triangle and returns appropriate Boolean values using 'strtok'. The program should ask to enter the 3 sides and deduce (True/False) whether the triangle is Isosceles, Equilateral...
#5: Question reopened by user avatar Alexei‭ · 2021-12-18T06:29:29Z (almost 3 years ago)
#4: Post edited by user avatar Trilarion‭ · 2021-12-17T13:04:41Z (almost 3 years ago)
completely reworked the question to contain the core idea and ask for an efficient algorithm/C implementation, removed the code example because it was not related to the core problem
  • How do you remove every occurrence of the second string inside the first one?
  • How to efficiently remove every occurrence of one string inside another
  • I came across a sample practice question on the internet. How do you remove every occurence of the second string inside the first one?
  • > Write a program that reads two strings of less than 30 characters and removes
  • every occurrence of the second string inside the first one. After each removal, the
  • remaining part of the first string should be shifted to the left, a number of places
  • equal to the characters of the second string. The program should display the first
  • string, before it ends. Here's my progress so far:
  • ```#include <stdio.h>
  • #include <stdlib.h>
  • #include <string.h>
  • char *new_string(char *, char *);
  • int main(void)
  • {
  • char *str1, *str2, *ptr3;
  • str1 = malloc(30 * sizeof(char));
  • printf("Enter first text: ");
  • fgets(str1, 30, stdin);
  • str2 = malloc(30 * sizeof(char));
  • printf("Enter second text: ");
  • fgets(str2, 30, stdin);
  • ptr3 = new_string(str1, str2);
  • if (ptr3 == NULL)
  • printf("String 1 has been removed totally");
  • else {
  • printf("");
  • }
  • return 0;
  • }
  • char *new_string(char *str1, char *str2){
  • //need some help here
  • }
  • ```
  • I have two strings and want to remove every occurrence of the second string inside the first one starting from the beginning of the first string and as if each occurrence was removed immediately.
  • I would like to to so in C in a time efficient way, i.e. probably with the lowest number of comparisons and include/copy actions.
#3: Question closed by user avatar Lundin‭ · 2021-12-13T10:24:47Z (almost 3 years ago)
#2: Post edited by user avatar dumplings‭ · 2021-12-13T08:15:43Z (almost 3 years ago)
  • I came across a sample practice question on the internet. How do you remove every occurence of the second string inside the first one?
  • > Write a program that reads two strings of less than 30 characters and removes
  • every occurrence of the second string inside the first one. After each removal, the
  • remaining part of the first string should be shifted to the left, a number of places
  • equal to the characters of the second string. The program should display the first
  • string, before it ends. Here's my progress so far:
  • ```#include <stdio.h>
  • #include <stdlib.h>
  • #include <string.h>
  • char *new_string(char *, char *);
  • int main(void)
  • {
  • char *str1, *str2, *ptr3;
  • str1 = malloc(30 * sizeof(char));
  • printf("Enter first text: ");
  • fgets(str1, 30, stdin);//must use fgets
  • str2 = malloc(30 * sizeof(char));
  • printf("Enter second text: ");
  • fgets(str2, 30, stdin);//must use fgets
  • ptr3 = new_string(str1, str2);
  • if (ptr3 == NULL)
  • printf("String 1 has been removed totally");
  • else {
  • printf("");
  • }
  • return 0;
  • }
  • char *new_string(char *str1, char *str2){
  • //need some help here
  • }
  • ```
  • I came across a sample practice question on the internet. How do you remove every occurence of the second string inside the first one?
  • > Write a program that reads two strings of less than 30 characters and removes
  • every occurrence of the second string inside the first one. After each removal, the
  • remaining part of the first string should be shifted to the left, a number of places
  • equal to the characters of the second string. The program should display the first
  • string, before it ends. Here's my progress so far:
  • ```#include <stdio.h>
  • #include <stdlib.h>
  • #include <string.h>
  • char *new_string(char *, char *);
  • int main(void)
  • {
  • char *str1, *str2, *ptr3;
  • str1 = malloc(30 * sizeof(char));
  • printf("Enter first text: ");
  • fgets(str1, 30, stdin);
  • str2 = malloc(30 * sizeof(char));
  • printf("Enter second text: ");
  • fgets(str2, 30, stdin);
  • ptr3 = new_string(str1, str2);
  • if (ptr3 == NULL)
  • printf("String 1 has been removed totally");
  • else {
  • printf("");
  • }
  • return 0;
  • }
  • char *new_string(char *str1, char *str2){
  • //need some help here
  • }
  • ```
#1: Initial revision by user avatar dumplings‭ · 2021-12-13T08:15:14Z (almost 3 years ago)
How do you remove every occurrence of the second string inside the first one?
I came across a sample practice question on the internet. How do you remove every occurence of the second string inside the first one?
 > Write a program that reads two strings of less than 30 characters and removes 
every occurrence of the second string inside the first one. After each removal, the 
remaining part of the first string should be shifted to the left, a number of places 
equal to the characters of the second string. The program should display the first 
string, before it ends. Here's my progress so far:

```#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
char *new_string(char *, char *); 
int main(void) 
{ 
 char *str1, *str2, *ptr3; 
 str1 = malloc(30 * sizeof(char)); 
 printf("Enter first text: "); 
 fgets(str1, 30, stdin);//must use fgets 
 str2 = malloc(30 * sizeof(char)); 
 printf("Enter second text: "); 
 fgets(str2, 30, stdin);//must use fgets 
 ptr3 = new_string(str1, str2); 
 if (ptr3 == NULL) 
 printf("String 1 has been removed totally"); 
 else { 
    printf("");
 } 
 return 0; 
} 
char *new_string(char *str1, char *str2){ 
//need some help here
}
```