concatenation of two strings omitting overlapping string

Problem
#include <stdio.h>
#include <string.h>

char *Strcat(char *dst, const char *src)
{
size_t dstLen = strlen(dst);
size_t srcLen = strlen(src);


char *p = dst + dstLen + srcLen; /* Pointer to the end of the concatenated string */

const char *q = src + srcLen - 1; /* Pointer to the last character of the src */

char *r = dst + dstLen - 1; /* Temp Pointer to the last character of the dst*/

char *end = r; /* Permenent Pointer to the last character of the dst*/

*p = ''; /*terminating the concatened string with NULL character */


while(q >= src) /*Copy src in reverse*/
{
if(*r == *q) /*Till it matches with the src, decrement r */
{
r--;
}
else
                {

r = end;
                        if(*r == *q) 
                        {

r--;
}
                }

*p-- = *q--; } while(r >= dst) /*Copy dst, ending with r */ *p-- = *r--; return p + 1;} int main(){ char a[64] = "hello everyone"; const char *b = "everyone is good"; printf("%s\n", Strcat(a, b));

   
return 0;

}
Advertisements

7 thoughts on “concatenation of two strings omitting overlapping string

  1. Hi Sambasiva,Thanks for the post.. But I have few doubts..1. I am finding it difficult to understand the prog.2.char *p = dst + dstLen + srcLen; /* Pointer to the end of the concatenated string */ — By this line how the strings will be concatenated?I am sending you my prog..Can you pls see and tell me the correctness?#include #include Void omit(char *src){ int i; for(i=0; *(src+i);i++) // loop to go thru src string char by char If(*(src+i) == ‘ ‘) // this if is for first space…so “everyone” will be removed from src string Break; Src ++; // increment the src string to point to “is good” after space} int main(){ char a[64] = "hello everyone"; char *b = "everyone is good"; omit(*b); printf("%s\n", Strcat(a, b));}

  2. Hi Anshuman, The declaration part is setting up the necessary variables. The actual copy happens in "while" loops. The algorithm copies in reverse order. first src, then dst. Meanwhile, it omits the common string at the end of the dst and beginning of the src

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s