Question
write a c program that displays a diamond pattern, when a user enters
an odd number between 0 and 19, and displays on the screen
/* BEGIN new.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void function(char *array, int max, int limit);
char *str_rev(char *s);
int main(void)
{
char *array;
int counter, rc, input, im1;
puts("enter an odd number between 0 and 19");
rc = scanf("%d", &input);
if (rc == 1 && input > 0 && 20 > input && input % 2 == 1) {
counter = input;
array = calloc(1, input);
if (array != NULL) {
im1 = input - 1;
while (counter-- != 0) {
function(array, im1, counter);
}
for (counter = 0; counter != input; ++counter) {
function(array, im1, counter);
}
} else {
puts("array == NULL");
}
free(array);
} else {
puts("bad input");
}
return 0;
}
void function(char *array, int max, int limit)
{
int count;
for (count = 0; count != limit; ++count) {
array[count] = ' ';
}
while (count != max) {
array[count] = '*';
++count;
}
fputs(array, stdout);
putchar('*');
str_rev(array);
puts(array);
}
char *str_rev(char *s)
{
char *t, swap;
char *const p = s;
if (s[0] != '\0' && s[1] != '\0') {
t = s + 1 + strlen(s + 2);
do {
swap = *t;
*t-- = *s;
*s++ = swap;
} while (t > s);
}
return p;
}
/* END new.c */