|
-
Nov 12th, 2001, 01:42 PM
#1
Thread Starter
Addicted Member
string insert
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"
-
Nov 12th, 2001, 02:23 PM
#2
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);
}
-
Nov 12th, 2001, 03:48 PM
#3
transcendental analytic
Well, this thing you can use on various type of arrays
PHP Code:
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);
};
Last edited by kedaman; Nov 12th, 2001 at 03:51 PM.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 12th, 2001, 04:00 PM
#4
transcendental analytic
ok, here's another one i just made, it supports deep copying (using the copyconstructor of a class)
PHP Code:
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;
};
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 12th, 2001, 04:15 PM
#5
transcendental analytic
and for C style strings, you can use this shortcut:
PHP Code:
char* insert(char* ds,char* is){ return insert(ds,ds+strlen(ds)-1,is,is+strlen(is)-1); };
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|