Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8637729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:32:46+00:00 2026-06-12T10:32:46+00:00

I wish to insert some characters into a string in C: Example: char string[100]

  • 0

I wish to insert some characters into a string in C:

Example: char string[100] = "20120910T090000";

I want to make it something like "2012-09-10-T-0900-00"

My code so far:

void append(char subject[],char insert[], int pos) {
    char buf[100]; 

    strncpy(buf, subject, pos); 

    int len = strlen(buf);

    strcpy(buf+len, insert); 

    len += strlen(insert);  

    strcpy(buf+len, subject+pos); 
    strcpy(subject, buf);   
}

When I call this the first time I get: 2012-0910T090000

However when I call it a second time I get: 2012-0910T090000-10T090000

Any help is appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:32:47+00:00Added an answer on June 12, 2026 at 10:32 am

    Here’s some working code, which gives me the output:

    String: <<20120910T090000>>
    String: <<2012-0910T090000>>
    String: <<2012-09-10T090000>>
    String: <<2012-09-10-T090000>>
    String: <<2012-09-10-T-090000>>
    String: <<2012-09-10-T-0900-00>>
    

    It uses memmove() because it is guaranteed that the strings being copied around overlap.

    #include <assert.h>
    #include <string.h>
    #include <stdio.h>
    
    static void insert(char *str, size_t len, char c, size_t pos)
    {
        memmove(&str[pos+1], &str[pos], len - pos + 1);
        str[pos] = c;
    }
    
    int main(void)
    {
        char string[25] = "20120910T090000";
        // I want to make it something like "2012-09-10-T-0900-00"
        char inschr[] = "-----";
        int  inspos[] = { 4, 7, 10, 12, 17 };
        enum { NUMCHR = sizeof(inschr) / sizeof(inschr[0]) };
        enum { NUMPOS = sizeof(inspos) / sizeof(inspos[0]) };
        assert(NUMCHR == NUMPOS + 1);
        size_t length = strlen(string);
    
        printf("String: <<%s>>\n", string);
        for (int i = 0; i < NUMPOS; i++)
        {
            insert(string, length, inschr[i], inspos[i]);
            length++;
            printf("String: <<%s>>\n", string);
        }
        return(0);
    }
    

    Of course, I’m assuming C99 support with the for loop notation. Note, too, that in classic C style, the code does not take the size of the target string, so it does not ensure that there is no buffer overflow. It wouldn’t be all that hard to add the parameter and do the check; the problem area is how to indicate that the function failed. You could use a different interface to the function, taking arbitrary length strings to be inserted at arbitrary positions; it isn’t significantly harder…

    String: <<20120910T090000>>
    String: <<2012-0910T090000>>
    String: <<2012-09-10T090000>>
    String: <<2012-09-10-T090000>>
    String: <<2012-09-10-T-090000>>
    String: <<2012-09-10-T-0900-00>>
    
    #include <assert.h>
    #include <string.h>
    #include <stdio.h>
    
    static int insert(char *str, size_t max, size_t len, char *ins, size_t pos)
    {
        assert(str[len] == '\0');
        assert(len < max);
        size_t inslen = strlen(ins);
        if (len + inslen + 1 >= max)
            return -1;
        memmove(&str[pos+inslen], &str[pos], len - pos + inslen);
        memmove(&str[pos], ins, inslen);
        return len + inslen;
    }
    
    int main(void)
    {
        char string[25] = "20120910T090000";
        // I want to make it something like "2012-09-10-T-0900-00"
        char *insstr[] = { "-", "-", "-", "-", "-" };
        int   inspos[] = {   4,   7,  10,  12,  17 };
        enum { NUMSTR = sizeof(insstr) / sizeof(insstr[0]) };
        enum { NUMPOS = sizeof(inspos) / sizeof(inspos[0]) };
        size_t length = strlen(string);
        assert(NUMSTR == NUMPOS);
    
        printf("String: <<%s>>\n", string);
        for (int i = 0; i < NUMPOS; i++)
        {
            int newlen = insert(string, sizeof(string), length, insstr[i], inspos[i]);
            if (newlen < 0)
            {
                printf("Oops! failed to insert [%s] into [%s]\n", insstr[i], string);
                break;
            }
            length = newlen;
            printf("String: <<%s>>\n", string);
        }
        return(0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish to DELETE the data from a table before performing an INSERT INTO,
I wish HTML could do something semantically equivalent to this; <dl class=main-list> <definitionitem> <dt>Some
I have the following xml and want to insert additional xml into it: <root>
I wish to do this in PHP find some words contain in a string
I wish to migrate some data from a single table into these new THREE
I wish to insert a Date value in DataRow[] and find a DateTime value
I can insert annotations on specific datasets on the graph but I wish to
I wish to use sp_start_job to start a job from within an insert trigger
I wish to make the uploaded file contents only viewable on the browser i.e
I wish to Insert or Update a row in a table - so I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.