Nishu
Nishu

Reputation: 191

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 this code, i am taking two arrays a and b, and after sorting them linearly, i am making an array c by merging a and b. Now i am attaching an image showing sample i/p and o/p. I am not able to get why there is < character at the end of o/p.

enter image description here

Upvotes: 0

Views: 60

Answers (1)

BLUEPIXY
BLUEPIXY

Reputation: 40145

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);

Upvotes: 1

Related Questions