#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_MAX (49)
static void memrev(char* const pLHS, char* const pRHS)
{
assert(pLHS < pRHS);
char* lhs = pLHS - 1;
char* rhs = pRHS + 1;
char t;
while ( ++lhs < --rhs )
{
t = *lhs;
*lhs = *rhs;
*rhs = t;
}
}
static char* strrev(char* const string)
{
memrev(string, &string[strlen(string) - 1]);
return string;
}
int main(void)
{
char string[BUFFER_MAX + 1];
char* pRHS;
char* pLHS;
// WARINING: The use of 'gets' is problematic in that 'gets' has no way of
// knowing the length of the buufer 'string'
printf("Enter some text (max %d characters): ", BUFFER_MAX);
gets(string);
printf("%s \n", string);
printf("%s \n", strrev(string));
strrev(string);
fflush(stdin);
pLHS = pRHS = string;
while ( *pRHS )
{
pLHS = pRHS;
while ( isspace(*pLHS++) ) { ; }
pRHS = --pLHS;
while ( *++pRHS && !isspace(*pRHS) ){ ; }
memrev(pLHS, pRHS - 1);
}
printf("%s \n", string);
return EXIT_SUCCESS;
}