#define MAX_STR 100
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* Reverse(char* s) {
char* result = NULL;
int left_index, right_index;
char ch;
int strsize = strlen(s)+1;
result = (char*) malloc(strsize);
if (result == NULL) {
exit(EXIT_FAILURE);
}
strncpy(result, s, strsize);
left_index = 0;
right_index = strlen(result) - 1;
while (left_index < right_index) {
ch = result[left_index];
result[left_index] = result[right_index];
result[right_index] = ch;
++left_index; --right_index;
}
return result;
}
int main() {
char line[MAX_STR];
char* rev_line;
printf("Please enter a string: ");
fgets(line, MAX_STR, stdin);
line[strlen(line)-1] = '\0';
rev_line = Reverse(line);
printf("The original string was: >%s<\n", line);
printf("Backwards, that string is: >%s<\n", rev_line);
printf("Thank you for trying our program.\n");
free(rev_line);
return EXIT_SUCCESS;
}