-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome.c
163 lines (152 loc) · 4.05 KB
/
palindrome.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
EXAMPLE:
SATOR
AREPO
TENET
OPERA
ROTAS
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#define ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define ALPHABET_LENGTH 26
#define PALINDROME_DEFAULT "IRONX"
const char *progname;
void usage();
char **new_palindrome(char *name, unsigned int length);
void print_palindrome(char **palindrome, unsigned int length);
void free_palindrome(char **palindrome, unsigned int length);
char rand_char();
char *rand_string(unsigned int length);
unsigned int summary(unsigned int n);
/**
* The main function
* @param argc Count of arguments
* @param argv Array of arguments
* @return 0 if done, otherwise > 0
*/
int main(int argc, char* argv[]) {
unsigned int i=0, length; char *name; char c;
progname = argv[0];
if (argc < 2) {
name = PALINDROME_DEFAULT;
} else {
name = argv[1];
if (strcmp(name, "-h") == 0 || strcmp(name, "--help") == 0) {
usage();
return 0;
}
while( name[i] ) {
c = toupper(name[i]);
if (c < 'A' || 'Z' < c) {
printf("Character not valid: %c\n\n", c);
return 1;
}
name[i] = c; i++;
}
}
length = (unsigned int) strlen(name);
srand(time(NULL));
char **palindrome = new_palindrome(name, length);
printf("\nPalindrome of \"%s\":\n\n", name);
print_palindrome(palindrome, length);
free_palindrome(palindrome, length); // Clear memory
printf("\n");
}
/**
* The usage method
* @return void
*/
void usage() {
printf("Usage: %s <string>\n\n", progname);
}
/**
* Generates a new palindrome
* @param name The starter string
* @param length The length of the string
* @return A new palindrome (array of string)
*/
char **new_palindrome(char *name, unsigned int length) {
unsigned int i, j, x, y, x_last, y_last; char **palindrome;
// Allocate space for n string pointers (n=length)
palindrome = (char **) malloc(sizeof(char*)*length);
palindrome[0] = name;
palindrome[length-1] = name;
// Allocate space for 5 characters in each line
for (i=0; i<length; i++) {
palindrome[i] = malloc(sizeof(char)*length);
}
y = 0;
y_last = length-1;
for (i=0; i<length; i++) {
x_last = i;
x = length-1-i;
// Vertical
palindrome[x][y] = name[x];
palindrome[x_last][y_last] = name[x];
// Horizontal
palindrome[y][x] = name[x];
palindrome[y_last][x] = name[x_last];
}
if (length > 2) {
unsigned int inner_length = length-2;
char *rand_inner = rand_string(inner_length);
char **inner_palindrome = new_palindrome(rand_inner, inner_length);
for (i=0; i<inner_length; i++) {
for (j=0; j<inner_length; j++) {
palindrome[j+1][i+1] = inner_palindrome[i][j];
}
}
free_palindrome(inner_palindrome, inner_length);
free(rand_inner);
}
return palindrome;
}
/**
* Clean a palindrome from RAM
* @param palindrome A palindrome
* @param length The length of the palindrome's start string
* @return void
*/
void free_palindrome(char **palindrome, unsigned int length) {
unsigned int i;
for (i=0; i<length; i++) {
free(palindrome[i]);
}
free(palindrome);
}
/**
* Print a palindrome
* @param palindrome A palindrome
* @param length The length of the palindrome's start string
* @return void
*/
void print_palindrome(char **palindrome, unsigned int length) {
unsigned int i;
for (i=0; i<length; i++) {
printf("%s\n", palindrome[i]);
}
}
/**
* Generate a random character
* @return The random character
*/
char rand_char() {
return ALPHABET[rand() % ALPHABET_LENGTH];
}
/**
* Generate a random string
* @param length The string length
* @return The random string
*/
char *rand_string(unsigned int length) {
unsigned int i;
char *r_string = (char *) malloc(sizeof(char)*length);
for (i=0; i<length; i++) {
r_string[i] = rand_char();
}
return r_string;
}