An example of cross merging of two strings in C language

Time:2022-5-5

I’ll stop talking nonsense and go straight to the code!

//Here are two methods
#include<stdio.h>
#include<string.h>
#define M 20 
void main() 
{ 
 char str1[M],str2[M],newstr[2*M]; 
 int chang1,chang2,i,flag,j; 
 int low,high; 
 Printf ("please enter the first string: \ n");
 gets(str1);
 Printf ("please enter the second string: \ n");
 gets(str2);
 chang1=strlen(str1);
 chang2=strlen(str2);
 if(chang1<chang2)
 {
 low=chang1;
 high=chang2;
 flag=1;
 } 
 else 
 {
 low=chang2;
 high=chang1;
 flag=0;
 } 
 For (I = 0; I < low; I + +) // copy the part with a small number of strings 
 { 
 newstr[2*i]=str1[i]; 
 newstr[2*i+1]=str2[i]; 
 } 
 For (I = low, j = 2 * I; I < high; I + +) // copy the redundant part of the string 
 {  
 if(flag)
  newstr[j++]=str2[i]; 
 else 
  newstr[j++]=str1[i]; 
 } 
 newstr[low+high]='
//Here are two methods
#include<stdio.h>
#include<string.h>
#define M 20 
void main() 
{ 
char str1[M],str2[M],newstr[2*M]; 
int chang1,chang2,i,flag,j; 
int low,high; 
Printf ("please enter the first string: \ n");
gets(str1);
Printf ("please enter the second string: \ n");
gets(str2);
chang1=strlen(str1);
chang2=strlen(str2);
if(chang1<chang2)
{
low=chang1;
high=chang2;
flag=1;
} 
else 
{
low=chang2;
high=chang1;
flag=0;
} 
For (I = 0; I < low; I + +) // copy the part with a small number of strings 
{ 
newstr[2*i]=str1[i]; 
newstr[2*i+1]=str2[i]; 
} 
For (I = low, j = 2 * I; I < high; I + +) // copy the redundant part of the string 
{  
if(flag)
newstr[j++]=str2[i]; 
else 
newstr[j++]=str1[i]; 
} 
newstr[low+high]='\0';// Add an end flag 
puts(newstr); 
} 
///////////////////////////////////////////////////
////////////////This is simpler than the one above//////////////////
#include<stdio.h>
int main()
{
char s1[20],s2[20],s3[40];
gets(s1);gets(s2);
int i=0,j=0;
while(s1[j]!='\0'&&s2[j]!='\0')
{
s3[i++]=s1[j];
s3[i++]=s2[j++];
}
if(s1[j]=='\0')
{
while(s2[j]!='\0')
s3[i++]=s2[j++];
}
else
{
while(s1[j]!='\0')
s3[i++]=s1[j++];
}
s3[i]='\0';
puts(s3);
printf("\n");
return 0;
}
';// Add an end flag puts(newstr); } /////////////////////////////////////////////////// ////////////////This is simpler than the one above////////////////// #include<stdio.h> int main() { char s1[20],s2[20],s3[40]; gets(s1);gets(s2); int i=0,j=0; while(s1[j]!='
//Here are two methods
#include<stdio.h>
#include<string.h>
#define M 20 
void main() 
{ 
char str1[M],str2[M],newstr[2*M]; 
int chang1,chang2,i,flag,j; 
int low,high; 
Printf ("please enter the first string: \ n");
gets(str1);
Printf ("please enter the second string: \ n");
gets(str2);
chang1=strlen(str1);
chang2=strlen(str2);
if(chang1<chang2)
{
low=chang1;
high=chang2;
flag=1;
} 
else 
{
low=chang2;
high=chang1;
flag=0;
} 
For (I = 0; I < low; I + +) // copy the part with a small number of strings 
{ 
newstr[2*i]=str1[i]; 
newstr[2*i+1]=str2[i]; 
} 
For (I = low, j = 2 * I; I < high; I + +) // copy the redundant part of the string 
{  
if(flag)
newstr[j++]=str2[i]; 
else 
newstr[j++]=str1[i]; 
} 
newstr[low+high]='\0';// Add an end flag 
puts(newstr); 
} 
///////////////////////////////////////////////////
////////////////This is simpler than the one above//////////////////
#include<stdio.h>
int main()
{
char s1[20],s2[20],s3[40];
gets(s1);gets(s2);
int i=0,j=0;
while(s1[j]!='\0'&&s2[j]!='\0')
{
s3[i++]=s1[j];
s3[i++]=s2[j++];
}
if(s1[j]=='\0')
{
while(s2[j]!='\0')
s3[i++]=s2[j++];
}
else
{
while(s1[j]!='\0')
s3[i++]=s1[j++];
}
s3[i]='\0';
puts(s3);
printf("\n");
return 0;
}
'&&s2[j]!='
//Here are two methods
#include<stdio.h>
#include<string.h>
#define M 20 
void main() 
{ 
char str1[M],str2[M],newstr[2*M]; 
int chang1,chang2,i,flag,j; 
int low,high; 
Printf ("please enter the first string: \ n");
gets(str1);
Printf ("please enter the second string: \ n");
gets(str2);
chang1=strlen(str1);
chang2=strlen(str2);
if(chang1<chang2)
{
low=chang1;
high=chang2;
flag=1;
} 
else 
{
low=chang2;
high=chang1;
flag=0;
} 
For (I = 0; I < low; I + +) // copy the part with a small number of strings 
{ 
newstr[2*i]=str1[i]; 
newstr[2*i+1]=str2[i]; 
} 
For (I = low, j = 2 * I; I < high; I + +) // copy the redundant part of the string 
{  
if(flag)
newstr[j++]=str2[i]; 
else 
newstr[j++]=str1[i]; 
} 
newstr[low+high]='\0';// Add an end flag 
puts(newstr); 
} 
///////////////////////////////////////////////////
////////////////This is simpler than the one above//////////////////
#include<stdio.h>
int main()
{
char s1[20],s2[20],s3[40];
gets(s1);gets(s2);
int i=0,j=0;
while(s1[j]!='\0'&&s2[j]!='\0')
{
s3[i++]=s1[j];
s3[i++]=s2[j++];
}
if(s1[j]=='\0')
{
while(s2[j]!='\0')
s3[i++]=s2[j++];
}
else
{
while(s1[j]!='\0')
s3[i++]=s1[j++];
}
s3[i]='\0';
puts(s3);
printf("\n");
return 0;
}
') { s3[i++]=s1[j]; s3[i++]=s2[j++]; } if(s1[j]=='
//Here are two methods
#include<stdio.h>
#include<string.h>
#define M 20 
void main() 
{ 
char str1[M],str2[M],newstr[2*M]; 
int chang1,chang2,i,flag,j; 
int low,high; 
Printf ("please enter the first string: \ n");
gets(str1);
Printf ("please enter the second string: \ n");
gets(str2);
chang1=strlen(str1);
chang2=strlen(str2);
if(chang1<chang2)
{
low=chang1;
high=chang2;
flag=1;
} 
else 
{
low=chang2;
high=chang1;
flag=0;
} 
For (I = 0; I < low; I + +) // copy the part with a small number of strings 
{ 
newstr[2*i]=str1[i]; 
newstr[2*i+1]=str2[i]; 
} 
For (I = low, j = 2 * I; I < high; I + +) // copy the redundant part of the string 
{  
if(flag)
newstr[j++]=str2[i]; 
else 
newstr[j++]=str1[i]; 
} 
newstr[low+high]='\0';// Add an end flag 
puts(newstr); 
} 
///////////////////////////////////////////////////
////////////////This is simpler than the one above//////////////////
#include<stdio.h>
int main()
{
char s1[20],s2[20],s3[40];
gets(s1);gets(s2);
int i=0,j=0;
while(s1[j]!='\0'&&s2[j]!='\0')
{
s3[i++]=s1[j];
s3[i++]=s2[j++];
}
if(s1[j]=='\0')
{
while(s2[j]!='\0')
s3[i++]=s2[j++];
}
else
{
while(s1[j]!='\0')
s3[i++]=s1[j++];
}
s3[i]='\0';
puts(s3);
printf("\n");
return 0;
}
') { while(s2[j]!='
//Here are two methods
#include<stdio.h>
#include<string.h>
#define M 20 
void main() 
{ 
char str1[M],str2[M],newstr[2*M]; 
int chang1,chang2,i,flag,j; 
int low,high; 
Printf ("please enter the first string: \ n");
gets(str1);
Printf ("please enter the second string: \ n");
gets(str2);
chang1=strlen(str1);
chang2=strlen(str2);
if(chang1<chang2)
{
low=chang1;
high=chang2;
flag=1;
} 
else 
{
low=chang2;
high=chang1;
flag=0;
} 
For (I = 0; I < low; I + +) // copy the part with a small number of strings 
{ 
newstr[2*i]=str1[i]; 
newstr[2*i+1]=str2[i]; 
} 
For (I = low, j = 2 * I; I < high; I + +) // copy the redundant part of the string 
{  
if(flag)
newstr[j++]=str2[i]; 
else 
newstr[j++]=str1[i]; 
} 
newstr[low+high]='\0';// Add an end flag 
puts(newstr); 
} 
///////////////////////////////////////////////////
////////////////This is simpler than the one above//////////////////
#include<stdio.h>
int main()
{
char s1[20],s2[20],s3[40];
gets(s1);gets(s2);
int i=0,j=0;
while(s1[j]!='\0'&&s2[j]!='\0')
{
s3[i++]=s1[j];
s3[i++]=s2[j++];
}
if(s1[j]=='\0')
{
while(s2[j]!='\0')
s3[i++]=s2[j++];
}
else
{
while(s1[j]!='\0')
s3[i++]=s1[j++];
}
s3[i]='\0';
puts(s3);
printf("\n");
return 0;
}
') s3[i++]=s2[j++]; } else { while(s1[j]!='
//Here are two methods
#include<stdio.h>
#include<string.h>
#define M 20 
void main() 
{ 
char str1[M],str2[M],newstr[2*M]; 
int chang1,chang2,i,flag,j; 
int low,high; 
Printf ("please enter the first string: \ n");
gets(str1);
Printf ("please enter the second string: \ n");
gets(str2);
chang1=strlen(str1);
chang2=strlen(str2);
if(chang1<chang2)
{
low=chang1;
high=chang2;
flag=1;
} 
else 
{
low=chang2;
high=chang1;
flag=0;
} 
For (I = 0; I < low; I + +) // copy the part with a small number of strings 
{ 
newstr[2*i]=str1[i]; 
newstr[2*i+1]=str2[i]; 
} 
For (I = low, j = 2 * I; I < high; I + +) // copy the redundant part of the string 
{  
if(flag)
newstr[j++]=str2[i]; 
else 
newstr[j++]=str1[i]; 
} 
newstr[low+high]='\0';// Add an end flag 
puts(newstr); 
} 
///////////////////////////////////////////////////
////////////////This is simpler than the one above//////////////////
#include<stdio.h>
int main()
{
char s1[20],s2[20],s3[40];
gets(s1);gets(s2);
int i=0,j=0;
while(s1[j]!='\0'&&s2[j]!='\0')
{
s3[i++]=s1[j];
s3[i++]=s2[j++];
}
if(s1[j]=='\0')
{
while(s2[j]!='\0')
s3[i++]=s2[j++];
}
else
{
while(s1[j]!='\0')
s3[i++]=s1[j++];
}
s3[i]='\0';
puts(s3);
printf("\n");
return 0;
}
') s3[i++]=s1[j++]; } s3[i]='
//Here are two methods
#include<stdio.h>
#include<string.h>
#define M 20 
void main() 
{ 
char str1[M],str2[M],newstr[2*M]; 
int chang1,chang2,i,flag,j; 
int low,high; 
Printf ("please enter the first string: \ n");
gets(str1);
Printf ("please enter the second string: \ n");
gets(str2);
chang1=strlen(str1);
chang2=strlen(str2);
if(chang1<chang2)
{
low=chang1;
high=chang2;
flag=1;
} 
else 
{
low=chang2;
high=chang1;
flag=0;
} 
For (I = 0; I < low; I + +) // copy the part with a small number of strings 
{ 
newstr[2*i]=str1[i]; 
newstr[2*i+1]=str2[i]; 
} 
For (I = low, j = 2 * I; I < high; I + +) // copy the redundant part of the string 
{  
if(flag)
newstr[j++]=str2[i]; 
else 
newstr[j++]=str1[i]; 
} 
newstr[low+high]='\0';// Add an end flag 
puts(newstr); 
} 
///////////////////////////////////////////////////
////////////////This is simpler than the one above//////////////////
#include<stdio.h>
int main()
{
char s1[20],s2[20],s3[40];
gets(s1);gets(s2);
int i=0,j=0;
while(s1[j]!='\0'&&s2[j]!='\0')
{
s3[i++]=s1[j];
s3[i++]=s2[j++];
}
if(s1[j]=='\0')
{
while(s2[j]!='\0')
s3[i++]=s2[j++];
}
else
{
while(s1[j]!='\0')
s3[i++]=s1[j++];
}
s3[i]='\0';
puts(s3);
printf("\n");
return 0;
}
'; puts(s3); printf("\n"); return 0; }

The above example of cross merging of two strings in C language is all the content shared by Xiaobian. I hope it can give you a reference and support developeppaer.