Sha256: c48d427fd71fb0f876b47f0841c756bfa56882c992d1186316608c435a1c9e1d

Contents?: true

Size: 1.27 KB

Versions: 19

Compression:

Stored size: 1.27 KB

Contents

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

#include "anagram.h"

char *lower_case(char *p)
{
   int len = strlen(p) + 1;
   char *lower = (char *)malloc(len);
   memcpy(lower, p, len);
   p = lower;
   while (*p) {
      *p = tolower(*p);
      p++;
   }

   return lower;
}

int compare(const void *a, const void *b)
{
   return *(const char *)a - *(const char *)b;
}

struct vector anagrams_for(char *in, struct vector vin)
{
   struct vector vout = {
      malloc(MAX_STR_LEN * sizeof(char *)),
      0
   };
   char (*vout_vecp)[MAX_STR_LEN] = vout.vec;

   char *lower = lower_case(in);
   char *sorted = malloc(strlen(in) + 1);
   memcpy(sorted, lower, strlen(in) + 1);
   qsort(sorted, strlen(sorted), 1, compare);

   char (*vecp)[MAX_STR_LEN] = vin.vec;
   int x;
   for (x = 0; x < vin.size; x++) {
      char *input_lower = lower_case((char *)vecp);
      if (strcmp(input_lower, lower) != 0) {
         char *input_sorted = input_lower;
         qsort(input_sorted, strlen(input_sorted), 1, compare);
         if (strcmp(sorted, input_sorted) == 0) {
            strcpy(*vout_vecp, *vecp);
            vout_vecp++;
            vout.size++;
         }
      }
      free(input_lower);
      vecp++;
   }

   free(lower);
   free(sorted);
   return vout;
}

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
trackler-2.2.1.61 tracks/c/exercises/anagram/src/example.c
trackler-2.2.1.60 tracks/c/exercises/anagram/src/example.c
trackler-2.2.1.59 tracks/c/exercises/anagram/src/example.c
trackler-2.2.1.58 tracks/c/exercises/anagram/src/example.c
trackler-2.2.1.57 tracks/c/exercises/anagram/src/example.c
trackler-2.2.1.56 tracks/c/exercises/anagram/src/example.c
trackler-2.2.1.55 tracks/c/exercises/anagram/src/example.c
trackler-2.2.1.54 tracks/c/exercises/anagram/src/example.c
trackler-2.2.1.53 tracks/c/exercises/anagram/src/example.c
trackler-2.2.1.52 tracks/c/exercises/anagram/src/example.c
trackler-2.2.1.51 tracks/c/exercises/anagram/src/example.c
trackler-2.2.1.50 tracks/c/exercises/anagram/src/example.c
trackler-2.2.1.49 tracks/c/exercises/anagram/src/example.c
trackler-2.2.1.48 tracks/c/exercises/anagram/src/example.c
trackler-2.2.1.47 tracks/c/exercises/anagram/src/example.c
trackler-2.2.1.46 tracks/c/exercises/anagram/src/example.c
trackler-2.2.1.45 tracks/c/exercises/anagram/src/example.c
trackler-2.2.1.44 tracks/c/exercises/anagram/src/example.c
trackler-2.2.1.43 tracks/c/exercises/anagram/src/example.c