Thread: manipulation of strings using pointer notation

  1. #1
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72

    Unhappy manipulation of strings using pointer notation

    in the following function,

    s1 is a source string. such as
    " for all and all for *1" taking note that the first character of this string is a space.

    s2 is the string that we wish to insert into s1.

    int n, is the position that we wish to insert s2.

    What I have works. It creates the string " onefor all and all for *1"

    how do I fix this?

    just a note: the "else" in this function has not been tested. I don't know what it will do yet. I've not gotten that far.

    insertstring(char *s1,char *s2, int n)
    {
    int len1 = strlen(s1);
    int len2 = strlen(s2);
    int i;
    char *p2 = s2;
    char *p1=s1+len1;

    //make a hole for to insert the string
    if(n > 0 )//if inserting between two chars in a string
    {
    for(i=0; i<(len1-n); ++i,--p1)
    {
    *p1=*(p1-len2);
    }
    }
    else //insert at the beginning of a string.
    for(i=0; i<=(len1-n); ++i,--p1) //if at the head of the sentence
    {
    *p1=*(p1-len2);
    }


    //insert the new string
    do{
    *p1 = *p2;
    if(*p2 != '\0') ++p1; ++p2;
    }
    while(*p2 != '\0');


    }

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    What is it exactly that you want to fix? The problem with spaces?
    1978 Silver Anniversary Corvette

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    If so, just do an extra iteration to create the space or append it on to the char *. Toggle with that.
    1978 Silver Anniversary Corvette

  4. #4
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    Originally posted by Garfield
    What is it exactly that you want to fix? The problem with spaces?
    Well, I'd like to fix that yes, But there is something that I am missing in my thinking...period. I've been looking at this simple function for 6 hours now and trying different things.
    Grrrr

    So if you can shed some light on the art of inserting characters in this manner it might help me figure it out on my own even.

    This function should be able to insert a string anywhere in the source. Either at the beginning, middle or end of the string. It should be intelligent enough to know to leave room for a space...or not.

    Thanks!!

    Mike

  5. #5
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    Originally posted by Garfield
    If so, just do an extra iteration to create the space or append it on to the char *. Toggle with that.
    That was the first thing I did try to make the extra space at the end of the string. then to move each item over that many spaces...
    *p= source+inserted+1 //+1 for the added space.

    Then I did
    *p = *(p-length) where length is the length of the string to insert.

    that causes other kinds of weirdness such as overwriting parts of the source string.

    and thanks for responding!!

    Mike

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Mmmm
    Code:
    #include <stdio.h>
    #include <string.h>
    
    // insert s2 into s1 at position pos
    void insertstring ( char *s1, char *s2, int pos ) {
        int len1 = strlen(s1);
        int len2 = strlen(s2);
        int i;
    
        // relocate the tail of s1
        // len1-pos chars need to be moved up len2 places
        // starting at position pos
        for ( i = len1 - pos ; i >= 0 ; i-- ) {
            s1[pos+len2+i] = s1[pos+i];
        }
    
        // copy s2 into the hole
        for ( i = 0 ; i < len2 ; i++ ) {
            s1[pos+i] = s2[i];
        }
    
        // restore the end of string
        s1[len1+len2] = '\0';
    }
    
    int main ( ) {
        char buff[100] = "hello world";
        insertstring( buff, "abc ", 6 );
        printf( "%s\n", buff );
        insertstring( buff, "foo ", 0 );
        printf( "%s\n", buff );
        return 0;
    }
    Shouldn't be too hard converting it to use pointer notation....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    Salem,
    thanks a gig! a tera even!

    This helped me tremendously. It not only did that, but it taught me something really good.

    Didn't some guy named Stee moderate this about a year ago?
    I am just wondering what happened to him. That's all.
    or...are you one in the same?

    Thanks agian,
    Mike

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Didn't some guy named Stee moderate this about a year ago?
    He did

    > are you one in the same?
    Yes
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Pointer Manipulation with strcat()
    By radiohead in forum C Programming
    Replies: 3
    Last Post: 03-03-2006, 07:17 AM
  4. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM
  5. pointer to pointers to arrays of strings??
    By Binkstone in forum C Programming
    Replies: 8
    Last Post: 09-14-2001, 02:56 AM