c - Array Sorting and Merging -


#include<stdio.h> #include<string.h>  int main(){     char a[10],b[10],temp;     int lena,lenb,i,j,k;     scanf("%s %s",&a,&b);     lena = strlen(a);     lenb = strlen(b);     char c[lena+lenb];     for(i=0;i<lena;i++){         for(j=0;j<lena;j++){             if(a[i]<a[j]){                 temp = a[i];                 a[i] = a[j];                 a[j] = temp;             }         }     }      for(i=0;i<lenb;i++){         for(j=0;j<lenb;j++){             if(b[i]<b[j]){                 temp = b[i];                 b[i] = b[j];                 b[j] = temp;             }         }     }     i=0;j=0;     for(k=0;k<(lena+lenb);k++){         if(i<lena && j<lenb){         if(a[i]<b[j]){             c[k]=a[i];i++;         }         else{             c[k]=b[j];j++;         }         }         else if(i==lena){             c[k]=b[j];             j++;         }         else if(j==lenb){             c[k]=a[i];             i++;         }       }     printf("%s",c); } 

using code, taking 2 arrays , b, , after sorting them linearly, making array c merging , b. attaching image showing sample i/p , o/p. not able why there < character @ end of o/p.

enter image description here

scanf("%s %s",&a,&b); --> scanf("%s %s", a, b);

char c[lena+lenb]; --> char c[lena+lenb+1];

printf("%s",c); --> c[k]='\0';printf("%s\n",c);


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -