Dr. C. Allen Butler (757)-727-7700 Voice
Daniel H. Wagner Assoc. (757)-722-0249 FAX
2 Eaton St., Ste. 500 allen@va.wagner.com
Hampton, VA 23669
-------------------------------------------------------------------
/***************************************************************************/
/* File : reverse.c */
/* Date : 03/28/98 */
/* Author : C. Allen Butler */
/* Project : CPSC395 Assignments */
/* Description: This function accepts a pointer to a string of characters*/
/* : allocates memory for the appropriate size string and */
/* : returns the string in reverse order. */
/***************************************************************************/
/*************************/
/* Include Files */
/*************************/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
char *reverse (char *in_string)
{
int len;
/* the length of the input string */
int i,j;
/* indices for string arrays */
char *out_string;
/* pointer to the output string */
/*******************************/
/* EXECUTABLE CODE BEGINS HERE */
/*******************************/
/* null strings are considered an irrecoverable error */
assert ((len = strlen (in_string)) > 0);
/* add one for null character */
out_string = malloc (len+1);
if (out_string == NULL)
{ printf ("Unable to allocate memory.\n");
return NULL;
}
/* reverse the input string */
for (i = 0, j = len-1; i < len; ++i, --j)
{ out_string [i] = in_string [j];
}
/* terminate the string */
out_string [len+1] = '\0';
return out_string;
}
------------------------------------------------------------------
/***************************************************************************/
/* File : reverse.c */
/* Date : 03/28/98 */
/* Author : C. Allen Butler */
/* Project : CPSC395 Assignments */
/* Description: This function accepts a pointer to an array of characters*/
/* : allocates memory for the appropriate size string and */
/* : returns the reverse of the string. */
/* Note : Since this function allocates the memory for the string */
/* : that is returned, it is incumbent upon the calling */
/* : function to free the allocated memory. */
/***************************************************************************/
/*************************/
/* Include Files */
/*************************/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
char *reverse (char *in_string)
{
int len;
/* the length of the input string */
char *out_string;
/* pointer to the output string */
/*******************************/
/* EXECUTABLE CODE BEGINS HERE */
/*******************************/
/* null strings are considered an irrecoverable error */
assert ((len = strlen (in_string)) > 0);
/* add one for null character */
out_string = malloc (len+1);
if (out_string == NULL)
{ printf ("Unable to allocate memory.\n");
return NULL;
}
/* set out_string to end of array */
out_string += len + 1;
/* put null character at end */
*out_string-- = '\0';
/* reverse the input string */
while (len--)
{ *out_string-- = *in_string++;
}
/* add one to out_string to correct last decrement */
return out_string+1;
}
-----------------------------------------------------------------
/***************************************************************************/
/* File : ip_rev.c */
/* Date : 03/28/98 */
/* Author : C. Allen Butler */
/* Project : CPSC395 Assignments */
/* Description: This function accepts a pointer to a string of characters*/
/* : and reverses the string in place. */
/***************************************************************************/
/*************************/
/* Include Files */
/*************************/
#include <string.h>
#include <stdlib.h>
void ip_reverse (char *in_string)
{
int len;
/* the length of the input string */
int i,j;
/* indices for string arrays */
char temp;
/* used to hold swapped value */
/*******************************/
/* EXECUTABLE CODE BEGINS HERE */
/*******************************/
len = strlen (in_string);
/* swap a character at a time working from the outside in */
for (i = 0, j = len-1; i < len/2; ++i, --j)
{ temp = in_string [j];
in_string [j] = in_string [i];
in_string [i] = temp;
}
}
-----------------------------------------------------------------
/***************************************************************************/
/* File : ip_rev.c */
/* Date : 03/28/98 */
/* Author : C. Allen Butler */
/* Project : CPSC395 Assignments */
/* Description: This function accepts a pointer to a string of characters*/
/* : and reverses the string in place. */
/***************************************************************************/
/*************************/
/* Include Files */
/*************************/
#include <string.h>
#include <stdlib.h>
void ip_reverse (char *in_string)
{
char *end;
/* pointer to the end of the input string */
char temp;
/* temporary variable used to swap characters */
/*******************************/
/* EXECUTABLE CODE BEGINS HERE */
/*******************************/
/* set to end of string */
end = in_string + strlen (in_string) - 1;
/* loop until pointers cross */
while (end > in_string)
{ temp = *end;
*end-- = *in_string;
*in_string++ = temp;
}
}