Use puts() for the version and help messages.
[sigilutils.git] / squares.h
blob861dc658a9a048baa440c367c70bd91490b40e72
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 + 1); // for NUL
15 int half = (length / 2) - 1; // account for 0-index
17 for (int i = 0; i < length; i++)
19 if (i == half)
20 result[i] = string[0];
22 else if (center && (i == (half + 1)))
23 result[i] = string[half + 1];
25 else if (i == (half + 1 + center))
26 result[i] = string[length - 1];
28 else if (i <= half)
29 result[i] = string[i + 1];
31 else
32 result[i] = string[i - 1];
35 return result;
39 char* generate_square(char* string)
41 int length = strlen(string);
42 // The size is for the square, the newlines and the NUL-term
43 int result_size = (length * length) + length;
44 char* result = calloc(sizeof(char), result_size);
46 char* current = string;
47 int count = 0;
48 int index = 0;
49 for (index = 0; index < (result_size / 2); index++)
51 if (count == length)
53 result[index] = '\n';
54 current = inside_out(current);
55 count = 0;
58 else
60 result[index] = current[count];
61 count++;
65 count = (result_size / 2) - 1;
66 for (index = (result_size / 2) - 1; index < result_size; index++)
68 // printf("\nprocessing half...\n");
69 result[index] = result[count];
70 count--;
73 return result;