Results 1 to 5 of 5

Thread: string insert

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183

    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"

  2. #2
    jim mcnamara
    Guest
    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);
    }

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Well, this thing you can use on various type of arrays
    PHP Code:
    template <class T>
    Tinsert(Tds,Tde,const Tis,const Tie){
        
    voidi=((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.

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    ok, here's another one i just made, it supports deep copying (using the copyconstructor of a class)
    PHP Code:
    template <class T>
    Tdeepinsert(Tds,Tde,const Tis,const Tie){
        for(
    Ti=ds,Tj=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.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    and for C style strings, you can use this shortcut:
    PHP Code:
    charinsert(chards,charis){   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
  •  



Click Here to Expand Forum to Full Width