Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdint.h>
- #include <string.h>
- void insert(int pos, char *s, const char *add) {
- int n = strlen(s), lenAdd = strlen(add);
- for (int i = n - 1; i >= pos; i--) {
- s[i + lenAdd] = s[i];
- }
- for (int i = 0; i < lenAdd; i++) {
- s[i + pos] = add[i];
- }
- }
- void del(int pos, int howMuch, char *s) {
- int n = strlen(s);
- for (int i = pos; i < n; i++) {
- s[i] = s[i + howMuch];
- }
- }
- uint32_t string_replace(char *where, const char *what, const char *replace) {
- uint32_t cnt = 0;
- char *f = strstr(where, what);
- const int lenRep = strlen(replace);
- const int lenWhat = strlen(what);
- while (f) {
- int pos = f - where;
- insert(pos, where, replace);
- del(pos + lenRep, lenWhat, where);
- cnt++;
- f = strstr(f + lenRep, what);
- }
- return cnt;
- }
- int main(void) {
- char s1[10003] = "", s2[31] = "", s3[31] = "";
- fgets(s1, 900, stdin);
- fgets(s2, 30, stdin);
- fgets(s3, 30, stdin);
- s1[strlen(s1) - 1] = 0;
- s2[strlen(s2) - 1] = 0;
- s3[strlen(s3) - 1] = 0;//*/
- uint32_t cnt = string_replace(s1, s2, s3);
- printf("found: %d\n", cnt);
- puts(s1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement