Click to See Complete Forum and Search --> : string insert
bob323
Nov 12th, 2001, 12:42 PM
Anyone ever coded a string insert function?
I tried, but it is getting way to messy any ideas?
char name [20];
char *p;
strcpy(name, "Bob Jackson");
p = strchar(name, " ");
insert the name say "Albert" so name is now "Bob Albert Jackson"
jim mcnamara
Nov 12th, 2001, 01:23 PM
Try:
[code]
// put insert into dest at pos position +1 ie., - keep pos
void insert(char *dest, char *insert, char pos){
char *buf;
char *s;
int i;
char tmp[132];
if (strchr(dest,pos)==NULL) return;
memset(tmp,'\0',sizeof(tmp) );
strcpy(tmp,dest);
buf = (char *)strchr(tmp,pos);
buf++;
s =insert;
for(i=0;i<strlen(insert);i++) *buf++ = *s++;
s= strchr(dest,pos);
s++;
while (*s!='\0') *buf++ = *s++;
*s='\0'; // make string null terminated
strcpy(dest,tmp);
}
kedaman
Nov 12th, 2001, 02:48 PM
Well, this thing you can use on various type of arrays
template <class T>
T* insert(T* ds,T* de,const T* is,const T* ie){
void* i=((void*)ie-(void*)is);
memcpy((void*)ds+i,(void*)ds,(void*)de-(void*)ds);
return memcpy((void*)ds,(void*)is,i);
};
kedaman
Nov 12th, 2001, 03:00 PM
ok, here's another one i just made, it supports deep copying (using the copyconstructor of a class)
template <class T>
T* deepinsert(T* ds,T* de,const T* is,const T* ie){
for(T* i=ds,T* j=ds+ie-is;i<=de;*j++=*i++);
for(i=ds,j=is;j<=ie;*i++=*j++);
return ds;
};
kedaman
Nov 12th, 2001, 03:15 PM
and for C style strings, you can use this shortcut:
char* insert(char* ds,char* is){ return insert(ds,ds+strlen(ds)-1,is,is+strlen(is)-1); };
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.