Use puts() for the version and help messages.
[sigilutils.git] / squares.h~
blobdb55c9e0b94ea42b64c1bee66942f647e870aca0
2 #include "basics.h"
3 #include <math.h>
5 int is_even (int num)
7   return (num % 2);
10 char* inside_out (char* string)
12   int length = strlen(string); 
13   char left_edge = string[0];
14   char right_edge = string[length - 1]; // account for 0-index
15   char left_cent;
16   char right_cent;
17   char* result;
18   int center = 0;
19   
20   if (length < 4)
21     return NULL;
23   if (is_even(length) != 0)
24     center = 1;
26   int half = (length / 2) - 1; // see above
27   
28   left_cent = string[half];
29   right_cent = string[half + 1 + center];
31   result = calloc(sizeof(char), length + 1);
32   memcpy(result, string, (size_t) length);
33   
34   result[0] = left_cent;
35   result[length - 1] = right_cent; // see above
36   result[half] = left_edge;
37   result[half + 1 + center] = right_edge;
38   return result;
42 char* generate_square(char* string)
44   int length = strlen(string);
45   // The size is for the square, the newlines and the NUL-term
46   char* result = calloc(sizeof(char), (length * length) + length);
48   char* current = string;
49   int newline_index = length + 1;
50   for (int index = 0; index < length; index++)
51     {
52       strcat(result, current);
53       result[newline_index - 1] = '\n';
54       newline_index += newline_index;
55       current = inside_out(current);
56     }
58   result[(length * length) + length - 1] = '\0';
59   return result;