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

int char_cmp(const void *pa, const void *pb)
{
    const char *a = pa;
    const char *b = pb;
    
    return *a - *b;
}

bool partial_anagram(const char *aa, const char *bb)
{
    char A[64];
    char B[64];
    
    const char *a = strcpy(A, aa);
    const char *b = strcpy(B, bb);
    
    qsort(A, strlen(A), 1, char_cmp);
    qsort(B, strlen(B), 1, char_cmp);
    
    while (*b) {
        while (*a && *a < *b) a++;
        
        if (*a != *b) return false;
        a++;
        b++;
    }
    
    return true;
}

static const char *bool_str(bool x)
{
    return x ? "true" : "false";
}

int test(const char *a, const char *b, bool expected)
{
    bool have = partial_anagram(a, b);
    
    printf("\"%s\" matches \"%s\": %s", a, b, bool_str(have));
    
    if (have != expected) {
        printf(", expected %s", bool_str(expected));
    }
    
    puts(".");

    return (have != expected);
}

int main(void)
{
    int misses = test("try", "try", true)
               + test("try", "rty", true)
               + test("try", "tray", false)
               + test("try", "", true)
               + test("try", "y", true)
               + test("try", "t", true)
               + test("try", "z", false)
               + test("try", "rytt", false)
               + test("try", "yt", true)
               + test("try", "yurt", false)
               + test("tree", "eert", true)
               + test("tree", "eeert", false);
               
    printf("\n%d misses.\n", misses);
    
    return 0;
}
