c - Combining two strings by removing duplicate substrings -


i have 2 strings combine, removing duplicate substrings. note every 2 consecutive numbers constitute substring. consider string str1 , str2:

str1 = "#100#123#100#678" str2 = "#100#678#100#56" 

i produce combined string as:

combostr = "#100#123#100#678#100#56" (i.e. removed duplicate #100#678) 

what's easiest way this? there way can achieve using regular expressions?

i don't think regular expressions way solve problem. regexes might useful in finding #123 tokens, problem needs backtrack on own string in way regex's references not desiged for.

i don't think there easy way (as in 3 lines of code) solve this.

i assume strings follow pattern (#\d+)* , pair created @ seam when joining 2 strings not trated special, i.e. resulting pair might considered duplicate. means can separate concatenation pair removal.

convert string list of integers, operate on these lists , join them back. that's work, makes actual code strip duplicates easier - it's complicated enough - , might come in handy when need operate on similar strings often.

#include <stdlib.h> #include <stdio.h>  /*  *      convert string list of @ max integers.  *      return value number of integers in list (which  *      max greater max!) or -1 if string invalid.  */ int ilist_split(int *ilist, int max, const char *str) {     const char *p = str;     int n = 0;      while (*p) {         int x;         int pos;          if (sscanf(p, "#%d %n", &x, &pos) < 1) return -1;         if (n < max) ilist[n] = x;         n++;         p += pos;     }      return n; }  /*  *      convert list of integers string. string  *      @ nbuf - 1 characters long , assured  *      zero-terminated if nbuf isn't 0. legal pass null  *      char buffer if nbuf 0. returns number of characters  *      have been written ha dthe buffer been long enough,  *      snprintf-style.  */ int ilist_join(const int *ilist, int n, char *buf, int nbuf) {     int len = 0;     int i;      (i = 0; < n; i++) {         len += snprintf(buf + len,              nbuf > len ? nbuf - len : 0, "#%d", ilist[i]);     }      return len; }  /*  *      auxliary function find pair in inteher list.  */ int ilist_find_pair(int *ilist, int n, int a1, int a2) {     int i;      (i = 1; < n; i++) {         if (ilist[i - 1] == a1 && ilist[i] == a2) return - 1;     }      return -1; }  /*  *      remove duplicate pairs integer list. first  *      pair kept, subsequent pairs deleted. returns  *      new length of array.  */ int ilist_remove_dup_pairs(int *ilist, int n) {     int i, j;      j = 1;     (i = 1; < n; i++) {         int a1 = ilist[i - 1];         int a2 = ilist[i];          if (ilist_find_pair(ilist, - 1, a1, a2) < 0) {             ilist[j++] = ilist[i];         } else {             i++;         }     }      return j; }    #define max 40  int main() {     const char *str1 = "#100#123#100#678";     const char *str2 = "#100#678#100#56";     char res[80];      int ilist[max];     int nlist;      /* convert str1 */     nlist = ilist_split(ilist, max, str1);     if (nlist > max) nlist = max;      /* convert , concatenate str2 */     nlist += ilist_split(ilist + nlist, max - nlist, str2);     if (nlist > max) nlist = max;      /* remove duplicate pairs */     nlist = ilist_remove_dup_pairs(ilist, nlist);      /* convert string */     ilist_join(ilist, nlist, res, sizeof(res));     printf("%s\n", res);      return 0; } 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -