Thread: Replacing part of a string?

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    6

    Replacing part of a string?

    I am trying to replace part of a string with a number. I am looking for "$$" within the string and I would like to change that to a number within the string. For instance, if the input was: "hello$$hello" then the replacement would look like this: "hello3125hello".

    The way I have been trying to get this done is by getting the part up to $$ into another char array, then get the part after $$ into a char array and combine everything using sprintf, something like this:
    Code:
    sprintf(fullstring, "%s%d%s", firstpart, id, secondpart);
    so far I have this:
    Code:
    char buffer[MAX_LEN];
    char firstpart[256];
    char secondpart[256];
    char fullstring[MAX_LEN];
    char *p;
    int id = 1222;
    
    printf(":");
    fgets(buffer, MAX_LEN, stdin);
    
    if((p = strstr(buffer, "$$")))
    {
    strncpy(firstpart, buffer, p-buffer);
    }
    when I print out firstpart, I get the string up to "$$" but I am not sure how to get the second part. How would I get everything after the $$ into the secondpart char array?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,234
    Your idea works, but you don't really need firstpart and secondpart as you can write directly to fullstring. Here's my example:
    Code:
    /* Interpolate src by substituting placeholder with the decimal string
     * representation of value, writing the result, including the terminating null
     * character, to the first n chars of dest, returning dest.
     */
    char *interpolate(char *dest,
                      size_t n,
                      const char *src,
                      const char *placeholder,
                      int value)
    {
        if (n > 0)
        {
            char *p = strstr(src, placeholder);
            if (p)
            {
                size_t first_segment_len = (size_t)(p - src);
                if (first_segment_len >= n)
                {
                    first_segment_len = n - 1;
                }
                dest[0] = '\0';
                strncat(dest, src, first_segment_len);
                snprintf(dest + first_segment_len,
                         n - first_segment_len,
                         "%d%s",
                         value,
                         p + strlen(placeholder));
            }
            else
            {
                dest[0] = '\0';
                strncat(dest, src, n - 1);
            }
        }
        return dest;
    }
    Applying the above to your example, you would call interpolate like this:
    Code:
    interpolate(fullstring, MAX_LEN, "hello$$hello", "$$", 123);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 03-17-2012, 09:36 PM
  2. Replacing a part of a string with an other???
    By pseudonoma in forum C Programming
    Replies: 10
    Last Post: 03-24-2008, 08:31 AM
  3. Replacing string in file
    By Spedge in forum C Programming
    Replies: 1
    Last Post: 08-19-2003, 02:53 AM
  4. replacing string in file except the first
    By jetfreggel in forum C Programming
    Replies: 4
    Last Post: 01-12-2003, 03:03 PM
  5. replacing a part of a string
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-20-2002, 03:32 AM

Tags for this Thread