Rewrote inside_out(), make sure chars shift
[sigilutils.git] / squares.h
blob54f841021595dd8f4ecb4f3549bd780bd2f9090c
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 int center = is_even(length);
14 char* result = calloc(sizeof(char), length + 2); // for newline and
15 // NUL
16 int half = (length / 2) - 1; // account for 0-index
18 for (int i = 0; i < length; i++)
20 if (i == half)
21 result[i] = string[0];
23 else if (center && (i == (half + 1)))
24 continue;
26 else if (i == (half + 1 + center))
27 result[i] = string[length - 1];
29 else if (i <= half)
30 result[i] = string[i + 1];
32 else
33 result[i] = string[i - 1];
36 result[length + 1] = '\n';
37 return result;
41 char* generate_square(char* string)
43 int length = strlen(string);
44 // The size is for the square, the newlines and the NUL-term
45 int result_size = (length * length) + length;
46 char* result = calloc(sizeof(char), result_size);
48 char* current = string;
49 for (int i = 0; i < length; i++)
51 strcat(result, current);
52 result[(length + 1) * (i + 1)] = '\n';
53 current = inside_out(current);
56 return result;