/*
 * This program reads a string from the standard input, reverses it,
 * then prints it
 */

#include <stdio.h>

/* swap *a and *b */

void swap (char *a, char *b) {
	char	t;

	t = *a;
	*a = *b;
	*b = t;
}

/*
 * Reverse a string.  The algorithm: swap the first character with the
 * last, the second with the next-to-last, etc. up to the middle of 
 * the string.
 */

void reverse (char s[]) {
	int	i, middle, length;

	/* length is the length of the string */

	length = strlen (s);

	/* middle is the middle of the string (doesn't matter
	 * if length is even or odd; in the case of an odd length
	 * string, the middle character remains the same 
	 */

	middle = length / 2;

	/* swap i'th with length-i-1'th */

	for (i=0; i<middle; i++) swap (&s[i], &s[length-i-1]);
}

int main () {
	char	s[100];
	int	i;

	/* prompt and get a string */

	printf ("enter a string\n");
	fgets (s, 100, stdin);

	/* get rid of carriage return */

	for (i=0; s[i]; i++) if (s[i] == '\n') s[i] = 0;

	/* reverse the string and print it out */

	reverse (s);
	printf ("%s\n", s);
}