Wednesday, 19 June 2019

Merge two strings picking a character alternatively from each string

#include<stdio.h>
#include <string.h>
int main() {
   char st1[100],st2[100],st3[200];
   scanf("%s%s",st1,st2);
   int l1,l2,i,k=0,j=0;
   l1=strlen(st1);
   l2=strlen(st2);

   for(i=0;j<l1 && k<l2;i++)
   {
       if(i%2 == 0)
            st3[i]=st1[j++];
       else
            st3[i]=st2[k++];
   }
   while(j<l1)
        st3[i++] = st1[j++];
   while(k<l2)
        st3[i++] = st2[k++];
   st3[i]='\0';
   printf("%s",st3);
}

No comments:

Post a Comment