Sha256: ce4b65c93b0a573f227e0e294ea17edf02802f72e5f0178a26eb5be7e3006811

Contents?: true

Size: 1.14 KB

Versions: 63

Compression:

Stored size: 1.14 KB

Contents

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

char **make_diamond(const char letter)
{
   if (!isalpha(letter))
      return NULL;

   const char letter_upper = toupper(letter);
   const size_t row_count = 2 * (letter_upper - 'A' + 1) - 1;
   const size_t col_count = row_count + 1;

   char **diamond = malloc(sizeof(char *) * row_count);
   if (diamond == NULL) {
      fprintf(stderr, "Memory error!\n");
      return NULL;
   }

   char *all_rows = malloc(row_count * col_count);
   if (all_rows == NULL) {
      fprintf(stderr, "Memory error!\n");
      free(diamond);
      return NULL;
   }

   memset(all_rows, ' ', row_count * col_count);

   const size_t middle = row_count / 2;
   char curr_char = 'A';
   int advance = 1;
   for (size_t row = 0; row < row_count; row++) {
      diamond[row] = &all_rows[row * col_count];
      diamond[row][col_count - 1] = '\0';

      diamond[row][middle + (curr_char - 'A')] = curr_char;
      diamond[row][middle - (curr_char - 'A')] = curr_char;

      if (curr_char == letter_upper)
         advance = -1;
      curr_char += advance;
   }

   return diamond;
}

Version data entries

63 entries across 63 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/c/exercises/diamond/src/example.c
trackler-2.2.1.179 tracks/c/exercises/diamond/src/example.c
trackler-2.2.1.178 tracks/c/exercises/diamond/src/example.c
trackler-2.2.1.177 tracks/c/exercises/diamond/src/example.c
trackler-2.2.1.176 tracks/c/exercises/diamond/src/example.c
trackler-2.2.1.175 tracks/c/exercises/diamond/src/example.c
trackler-2.2.1.174 tracks/c/exercises/diamond/src/example.c
trackler-2.2.1.173 tracks/c/exercises/diamond/src/example.c
trackler-2.2.1.172 tracks/c/exercises/diamond/src/example.c
trackler-2.2.1.171 tracks/c/exercises/diamond/src/example.c
trackler-2.2.1.170 tracks/c/exercises/diamond/src/example.c
trackler-2.2.1.169 tracks/c/exercises/diamond/src/example.c
trackler-2.2.1.167 tracks/c/exercises/diamond/src/example.c
trackler-2.2.1.166 tracks/c/exercises/diamond/src/example.c
trackler-2.2.1.165 tracks/c/exercises/diamond/src/example.c
trackler-2.2.1.164 tracks/c/exercises/diamond/src/example.c
trackler-2.2.1.163 tracks/c/exercises/diamond/src/example.c
trackler-2.2.1.162 tracks/c/exercises/diamond/src/example.c
trackler-2.2.1.161 tracks/c/exercises/diamond/src/example.c
trackler-2.2.1.160 tracks/c/exercises/diamond/src/example.c