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

  • SEARCH
  • Home
  • 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 6566075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:10:24+00:00 2026-05-25T14:10:24+00:00

In an interview, I was asked to write an implementation of strcpy and then

  • 0

In an interview, I was asked to write an implementation of strcpy and then fix it so that it properly handles overlapping strings. My implementation is below and it is very naive. How do I fix it so that:

  1. It detects overlapping strings and
  2. after detecting, how do we deal with the overlap and proceed?

char* my_strcpy(char *a, char *b) {

     if (a == NULL || b == NULL) {
         return NULL;
     }
     if (a > b) {
         //we have an overlap?
         return NULL;
     }
     char *n = a;

     while (*b != '\0') {
         *a = *b;
         a++;
         b++;
     }
     *a = '\0';
     return n;
}

int main(int argc, char *argv[])
{
    char str1[] = "wazzupdude";
    char *after_cpy = my_strcpy(str1 + 2, str1);
    return 0;
}

EDIT:

So one possible implementation based on @Secure’s answer is:

char* my_strcpy(char *a, char *b) {

    if (a == NULL || b == NULL) {
        return NULL;
    }

    memmove(a, b, strlen(b) + 1);
    return a;
}

If we don’t rely on memmove, then

char* my_strcpy(char *a, char *b) {

    if (a == NULL || b == NULL) {
        return NULL;
    }

    if (a == b) {
        return a;
    }

    // case1: b is placed further in the memory
    if ( a <= b && a + strlen(a) > b ) {
        char *n = a;

        while(*b != '\0') {
            *a = *b;
            a++; b++;
        }
        *a = '\0';
        return n;
    }

    // case 2: a is further in memory
    else if ( b <= a && b + strlen(b) > a ) { 
        char *src = b + strlen(b) - 1; // src points to end of b
        char *dest = a;

        while(src != b) {
            *dest = *src;
            dest--; src--;  // not sure about this..
        }
        *a = '\0';
        return a;
    }
}
  • 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-05-25T14:10:25+00:00Added an answer on May 25, 2026 at 2:10 pm

    There is no portable way to detect this. You have to do pointer comparisons, and these are only defined within the same object. I.e. if the two strings do not overlap and are in fact different objects, then the pointer comparisons give you undefined behaviour.

    I would let the standard library handle this, by using memmove(a, b, strlen(b) + 1).

    EDIT:

    As Steve Jessop pointed out in the comments, there actually is a portable but slow way to detect overlap in this case. Compare each address within b with the first and last address of a for equality. The equality comparison with == is always well defined.

    So you have something like this:

    l = strlen(b);
    isoverlap = 0;
    for (i = 0; i <= l; i++)
    {
        if ((b + i == a) || (b + i == a + l))        
        {
            isoverlap = 1;
            break;
        }
    }
    

    EDIT 2: Visualization of case 2

    You have something like the following array and pointers:

    S t r i n g 0 _ _ _ _ _ _ _
    ^       ^
    |       |
    b       a
    

    Note that b + strlen(b) results in a pointer to the terminating \0. Start one behind, else you need extra handling of edge cases. It is valid to set the pointers there, you just can’t dereference them.

    src = b + strlen(b) + 1;
    dst = a + strlen(b) + 1;
    
    S t r i n g 0 _ _ _ _ _ _ _
    ^       ^     ^       ^  
    |       |     |       |
    b       a     src     dst
    

    Now the copy loop which copies the \0, too.

    while (src > b)
    {
        src--; dst--;
        *dst = *src;
    }
    

    The first step gives this:

    src--; dst--;
    
    S t r i n g 0 _ _ _ _ _ _ _
    ^       ^   ^       ^  
    |       |   |       |
    b       a   src     dst
    
    *dst = *src;
    
    S t r i n g 0 _ _ _ 0 _ _ _
    ^       ^   ^       ^  
    |       |   |       |
    b       a   src     dst
    

    And so on, until src ends up equal to b:

    S t r i S t r i n g 0 _ _ _
    ^       ^              
    |       |            
    b       a          
    src     dst
    

    If you want it a bit more hackish, you could compress it further, but I don’t recommend this:

    while (src > b)
        *(--dst) = *(--src);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In an interview this is the question that is asked: You have to write
This is an interview Question that i was asked recently: Write a C program
I have been asked in an interview to write a SQL query which fetches
I was once asked of this in an interview: How to write a recursive
In a past interview, I was asked how would I write a mission critical
A friend of mine was asked, during a job interview, to write a program
Yesterday in my interview I was asked this question. (At that time I was
Today in my interview , i was asked to write a code to determine
This have been asked in the interview. How to write own dynamic_cast. I think,
Almost on every interview I'm asked some questions implies data structure implementation. Is there

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.