0
#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

4
  • 1
    printf("%s",c); : c isn't null-terminate.
    – BLUEPIXY
    Commented Apr 4, 2014 at 21:17
  • i dont get u, ll u plz elaborate.
    – Nishu
    Commented Apr 4, 2014 at 21:18
  • Finally, i am able to correct the error and getting expected o/p, but can u tell me, why it was happening ?
    – Nishu
    Commented Apr 4, 2014 at 21:22
  • String manipulation in C is made on the assumption that it is finished with '\0'. Access exception occurs or garbage should be displayed when it is not finished with '\0'.
    – BLUEPIXY
    Commented Apr 4, 2014 at 21:26

1 Answer 1

1

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

2
  • i have one more doubt, above program was working fine in following case: a = is b = it output = iist why there was no problem arise in this case ?
    – Nishu
    Commented Apr 4, 2014 at 21:35
  • 1
    @Nishu It seems to have no problem by luckily. (Perhaps, garbage is 0)
    – BLUEPIXY
    Commented Apr 4, 2014 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.