>i dont know it is correct or not
It's not. Why don't you test out thing on your compiler first before posting them?
To concatenate a string, remember that strings are basically arrays of characters. This means that you can loop through the arrays (or strings) and copy letters into another one. First of all, create an array that's the combined length of both strings (plus 1 for the terminating null character). Then simply start by copying the first string into the array, and then proceed by doing the same to the other.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
/* myconcat.c */
#include <stdio.h>
int main(void)
{
char first_s[] = "Hello";
char second_s[] = "World";
int i = 0;
int x = 0;
char long_s[sizeof(first_s) + sizeof(second_s)];
while(first_s[i])
{
long_s[i] = first_s[i];
++i;
}
long_s[i] = ' ';
++i;
while(second_s[x])
{
long_s[i] = second_s[x];
++i;
++x;
}
long_s[i] = '\0'; /* don't forget the null */
printf(long_s);
putchar('\n');
getchar();
return 0;
}
If you want to learn something more advanced, do read this little snippet by Dave Sinkula. http://www.daniweb.com/code/snippet406.html
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
#include
#include
#include
void main()
{
char str1[30],str2[30];
int l1,l2,i;
clrscr();
gets(str1);
gets(str2);
l1=strlen(str1);
l2=strlen(str2);
for(i=0;i<=l2;i++)
{
str1[l1+i]=str2[i];
}
printf("%s",str1);
}
The main function always returns an integer when hosted by an operating system. So void main() is not acceptable.
gets() is not acceptable neither, for different reason. It can not be stop of reading more that buffer can support, without overflow. #include<conio.h> and clrscr(); are none portable, therefore voids the "warranty". Furthermore, clearing the screen is a doubtful practice, for the sake of "prettiness".
Next time use the expected code tags to post source code. Here's an example .
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218