0
\$\begingroup\$

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 < 100

Here is my code:

#include <stdio.h>
#include <string.h>

char sort(int n, char str[])
{
    int i,j,swap;
    for(i=0;i<n-1;i++)
    {
        for(j=0;j<n-i-1;j++)
        {
            if(str[j]>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",&str1,&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?

\$\endgroup\$
  • \$\begingroup\$ What exactly do you mean by small? \$\endgroup\$ – janos Apr 22 '15 at 17:56
  • \$\begingroup\$ i mean shorter code, lesser number of lines \$\endgroup\$ – Atul Shanbhag Apr 22 '15 at 17:57
  • \$\begingroup\$ Use Ruby. Then you can squeeze the logic into one (1) line. \$\endgroup\$ – Frederick C. Lee Apr 22 '15 at 20:09
  • \$\begingroup\$ Personally I would prefer faster solutions or more scalable solutions to simply "shorter code." \$\endgroup\$ – twohundredping Apr 22 '15 at 20:58
  • \$\begingroup\$ "lesser number of lines" is a horrible measure of code performance or quality. Nothing is stopping you from writing this program on one single line. Instead, strive to make programs 1) readable and 2) effective in terms of speed & memory consumption. Whether or not you have to type a lot to achieve those things is irrelevant. \$\endgroup\$ – Lundin Apr 27 '15 at 12:59
1
\$\begingroup\$

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] > 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
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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