c - Formatting a string based on Input and predefined values -


i have 26 values's considering special symbol , special delimeter "$" value's can $a $z.

same time have predefined template as:

i have $a,$b,$c.....

now allowing user input string can contain special symbol , values of example: input - $acar $bbike $ctruck.

then output should : *i have car,bike,truck... *

as special symbol has been replaced values.

note 1.if $a car $a bike input value should take $a car rest should discarted.

if input string doesn't contain special symbol there should no change in output , output have $a,$b,$c.....

3.if input start i men $a glass till $a values should discarted.

which approach should follow make possible?

i thinking strstr on input string , compare special symbol , store position of special symbol in list , per position thinking take values don't think work me.

processing simplified using dynamic string.

like this

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h>  typedef struct dstr {     size_t size;     size_t capacity;     char *str; } dstr;//dynamic string  dstr *dstr_make(void){     dstr *s;     s = (dstr*)malloc(sizeof(dstr));     s->size = 0;     s->capacity=16;     s->str=(char*)realloc(null, sizeof(char)*(s->capacity += 16));     return s; }  void dstr_addchar(dstr *ds, const char ch){     ds->str[ds->size] = ch;     if(++ds->size == ds->capacity)         ds->str=(char*)realloc(ds->str, sizeof(char)*(ds->capacity += 16)); }  void dstr_addstr(dstr *ds, const char *s){     while(*s) dstr_addchar(ds, *s++);     //dstr_addchar(ds, '\0'); }  void dstr_free(dstr *ds){     free(ds->str);     free(ds); }  void dic_entry(char *dic[26], const char *source){     char *p, *backup, ch;      p = backup = strdup(source);      for(;null!=(p=strtok(p, " \t\n"));p=null){         if(*p == '$' && isupper(ch=*(p+1))){             if(dic[ch -'a'] == null)                 dic[ch -'a'] = strdup(p+2);         }     }     free(backup); }  void dic_clear(char *dic[26]){     int i;     for(i=0;i<26;++i){         if(dic[i]){             free(dic[i]);             dic[i] = null;         }     } }  int main(void){     const char *template = "i have $a,$b,$c.";     char *dic[26] = { 0 };     char buff[1024];     const char *cp;     dstr *ds = dstr_make();      printf("input special value setting: ");     fgets(buff, sizeof(buff), stdin);     dic_entry(dic, buff);      for(cp=template;*cp;++cp){         if(*cp == '$'){             char ch;             if(isupper(ch=*(cp+1)) && dic[ch - 'a']!=null){                 dstr_addstr(ds, dic[ch - 'a']);                 ++cp;             } else {                 dstr_addchar(ds, *cp);             }         } else {             dstr_addchar(ds, *cp);         }     }     dstr_addchar(ds, '\0');     printf("result:%s\n", ds->str);      dic_clear(dic);     dstr_free(ds);     return 0; } /* demo >a input special value setting: $acar $bbike $ctruck result:i have car,bike,truck.  >a input special value setting: $bbike result:i have $a,bike,$c. */ 

Comments

Popular posts from this blog

php - cannot display multiple markers in google maps v3 from traceroute result -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -