sir plz explain the following code of concatenation ....

3 replies [Last post]
amanhoney
Offline
Last seen: 9 years 40 weeks ago
Joined: 2010-01-08
bOt Points: 70

/* strcat.c */

 

# include<stdio.h>

 

char *strcat1(char *target, const char *source)

{

char *original = target;

while(*target)

target++;

while (*target++= *source++)

;

return(original);

}

 

main()

{

char Name[256]="Ram Bahadur ";

char Title[256]="Patel";

strcat1(Name, Title);

puts(Name);

shashwat
Offline
Last seen: 1 year 7 weeks ago
Joined: 2009-02-18
bOt Points: 1160
String Concatenation

/* strcat.c */

# include<stdio.h>

char *strcat1(char *target, const char *source)

{

/*

There is no need to use a third pointer *original in this case the whole thing can be done with two arguments only. I have commented your very first line since it it not needed.

*/

//char *original = target;

/*

In the next two lines target pointer is being incremented till it reaches the end of the array to which it is pointing

*/

        while(*target)

            target++;

/*

The next line is a very beautiful piece of code.

Note that right now target pointer is pointing at the very last element of the target array.

Now in the while condition the element to which source pointer is pointing is copied to the element where target pointer is pointing and both the pointers are incremented by one.

All this happens in a single line of code.

These process goes on until the source array is completely copied into the target array and we reach the end of the source array and while condition will now be false.

*/

 

while (*target++= *source++);

/*

After this your final array to which the target pointer was pointing is returned.

*/

        return(target);


}

int main()

{

char Name[256]="Ram Bahadur ";

        char Title[256]="Patel";

        strcat1(Name, Title);

        puts(Name);

        return 0;

}  

Founder and Admin

amanhoney
Offline
Last seen: 9 years 40 weeks ago
Joined: 2010-01-08
bOt Points: 70
concatenatino problem

thanks sir for ur help............... 

shashwat
Offline
Last seen: 1 year 7 weeks ago
Joined: 2009-02-18
bOt Points: 1160
You are welcome.  

You are welcome.  

Founder and Admin