Click here to Skip to main content
15,938,218 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have two strings one[ ] = "1234" and two[ ]= "5678". i need the output to be "15263748" soo can any one tell me how to get the desired output using c language ?

What I have tried:


int main()
	char one[]= "1357";
	char two[]= "2468";
	int i , j ;
	for (i=0;i<4;i++)

	return 0 ;
Updated 2-Jul-21 8:31am

Try to replace

printf("%c%c", one[i],two[j]);

and do not forget the semicolon and the end of line
Share this answer
When you print them out, don't you want the same index from both arrays? So, just use i instead of i and j.
Share this answer
Do not use strcat[^] like that: it does not, will not, and cannot increase the size of the destination string.

When you declare one like this:
char one[]= "1357";
You allocate 5 bytes of space on the stack, and fill it with your four characters and a terminating null. if you then try to use that as a destination, it will "run out" of characters and will overwrite the following data - in this case part of two - and at that point what happens starts to become very unpredictable.

You must allocate enough space in the destination to hold both strings when you use strcat or your application is very likely to crash.
Share this answer
Your definition of concatenation is confusing. Given 1234 and 5678, concatenation means append one string to the other: 12345678. That's what strcat will do. But you're showing alternating characters: 15263748. You need to write a loop to do that. Something like this:
char one[] = "1234";
char two[] = "5678";
int length = strlen(one) + strlen(two);
char[length + 1] result;

for(int s = 0, d = 0; d < length; ++s)
  if(s < strlen(one))
     result[d++] = one[s];
  if(s < strlen(two))
     result[d++] = two[s];

result[length] = '\0';
Share this answer
k5054 2-Jul-21 14:20pm    
One probably shouldn't call strlen() for each character placed in the result. If one or two or both are long, think several MB, then the performance of this will probably be very poor --- assuming the compiler isn't able to optimize away the strlen calls. Better to create a const int and assign the return value of strlen to it for len_one and len_two, then use those tokens in place of the strlen() calls. You could also do a little math ahead of time and only use the for loop to the min(len_one, len_two), and then use strcat to copy the rest of the other string. I'll leave the actual implementation to the OP, if they're inclined
Greg Utas 2-Jul-21 14:23pm    
Quite right. I thought about creating locals before the loop for this but figured it was a toy exercise. :)
k5054 2-Jul-21 14:39pm    
Playing around with compiler explorer it turns out gcc, clang and MSVC are all capable of optimizing away the strlen call. Even better, as near as I can tell, MSVC manages to inline the strlen call.
Read the documentation of strcat because you misuse this command.
In C you are responsible for providing the memory and you overwrite it. This is a severe bug and security flaw.
So learn the basic of memory and string in C++ before you move on or use the std::string class which does it for you.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900