Check if two strings are anagrams - HackerEarth You are given two strings. You have to check if the strings are permutations of each other and output "YES" or "NO" accordingly without quotes.
  
  Input:
  2 lines of input each containing a string. (a to z and 0 to 9) 
  
  Output:
  A single line containing YES or NO.
  
  Constraints:
  String would not contain any spaces. size of string &lt; 100 


Here is my code:

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

char sort(int n, char str[])
{
    int i,j,swap;
    for(i=0;i&lt;n-1;i++)
    {
        for(j=0;j&lt;n-i-1;j++)
        {
            if(str[j]&gt;str[j+1])
            {
                swap=str[j];
                str[j]=str[j+1];
                str[j+1]=swap;
            }
        }
    }
}
int main()
{
    char str1[100],str2[100];
    scanf("%s %s",&amp;str1,&amp;str2);
    sort(strlen(str1),str1);
    sort(strlen(str2),str2);
    if(strcmp(str1,str2)==0)
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}


How do I make this code as small (shorter code) as possible?
How do I make this code as small (shorter code) as possible?


You could change this:


if(strcmp(str1,str2)==0)
    printf("YES\n");
else
    printf("NO\n");



Like this, using the ternary operator and puts:

puts(strcmp(str1, str2) ? "NO" : "YES");


Technically,
you could also get rid of much of the whitespace and concatenate all instructions on a single line with semicolons.
But that's probably not what you really want.

If your aim is simplicity, then I think this is about as simple and short as it gets.

For readability, it would be better to add spaces around operators, for example like this:

    if (str[j] &gt; str[j+1])
    {
        swap = str[j];
        str[j] = str[j+1];
        str[j+1] = swap;
    }


Some minor final remarks:


bubble sort is a slow sorting algorithm, but with words of max 100 letters, it's probably good enough
uppercase and lowercase letters will be treated differently, but since the treatment of letter case is unspecified, that might be fine too