S
Stan Milam
I've been away from C programming since 1999 and I've recently taken it
up again. Below is a string function I wrote this morning that will be
going into my home-grown library of string functions. I brought the
idea of it from a function of the same name in PL/SQL. I thought it
would be handy in my C library. Mine differs in that the source string
argument is modified and returned from the function.
Working with C strings has always been problematic because you can crash
a program faster than you can say "Jack's you're uncle!" if you are not
careful. Some of my string functions I take the strcpy() approach and
copy in the modified string into a destination pointer that you can only
hope is pointing to something big enough and in your program's data
space. With others I modify the source string in place and return a
pointer to it. With this function I felt it safe to modify the string
in place because at worst one character would be changed to another and
at best the character would be removed from the source string, so there
is no chance of overflowing the buffer. Of course, someone will try to
use a string constant for the sorce string (i.e. "This is a string").
I thought I would post the code and see what the experts had to say. For
reference "strtools.h" is my header file for my homegrown string
functions, and chrsubst() is used to substitute characters in a string,
and str_rmchars() removes a set of characters from a string.
/**FUNCTION************************************************************/
/* Name: */
/* translate(). */
/* */
/* Synopsis: */
/* #include <strings.h> */
/* #include "strtools.h" */
/* char *translate(char *src, char *fromstr, char *tostr); */
/* */
/* Description: */
/* The translate function will translate characters that appear */
/* in both the src and fromstr strings. If the fromstr character */
/* has a corresponding value in the tostr string all characters */
/* in the src string will be translated to that of the */
/* corresponding tostr character. If there is no corresponding */
/* character in the tostr string the character will be removed */
/* from the source string. */
/* */
/* Arguments: */
/* char *source - The string to be examined and translated. */
/* char *fromstr- The characters to translate in the source. */
/* char *tostr - The corresponding characters the source */
/* characters are translated to. */
/* */
/* Return Value: */
/* The starting address of the source string. */
/* */
/************************************************************FUNCTION**/
char *
translate ( char *p_source, char *p_from_string, char *p_to_string )
{
unsigned x_sub = 0, l_len = strlen( p_to_string );
while ( *p_from_string ) {
if ( strchr( p_source, *p_from_string ) != NULL ) {
if ( x_sub < l_len )
chrsubst( p_source, *p_from_string, p_to_string[x_sub] );
else {
char l_ch[] = "";
l_ch[0] = *p_from_string;
str_rmchars( p_source, l_ch );
}
}
x_sub++;
p_from_string++;
}
return p_source;
}
up again. Below is a string function I wrote this morning that will be
going into my home-grown library of string functions. I brought the
idea of it from a function of the same name in PL/SQL. I thought it
would be handy in my C library. Mine differs in that the source string
argument is modified and returned from the function.
Working with C strings has always been problematic because you can crash
a program faster than you can say "Jack's you're uncle!" if you are not
careful. Some of my string functions I take the strcpy() approach and
copy in the modified string into a destination pointer that you can only
hope is pointing to something big enough and in your program's data
space. With others I modify the source string in place and return a
pointer to it. With this function I felt it safe to modify the string
in place because at worst one character would be changed to another and
at best the character would be removed from the source string, so there
is no chance of overflowing the buffer. Of course, someone will try to
use a string constant for the sorce string (i.e. "This is a string").
I thought I would post the code and see what the experts had to say. For
reference "strtools.h" is my header file for my homegrown string
functions, and chrsubst() is used to substitute characters in a string,
and str_rmchars() removes a set of characters from a string.
/**FUNCTION************************************************************/
/* Name: */
/* translate(). */
/* */
/* Synopsis: */
/* #include <strings.h> */
/* #include "strtools.h" */
/* char *translate(char *src, char *fromstr, char *tostr); */
/* */
/* Description: */
/* The translate function will translate characters that appear */
/* in both the src and fromstr strings. If the fromstr character */
/* has a corresponding value in the tostr string all characters */
/* in the src string will be translated to that of the */
/* corresponding tostr character. If there is no corresponding */
/* character in the tostr string the character will be removed */
/* from the source string. */
/* */
/* Arguments: */
/* char *source - The string to be examined and translated. */
/* char *fromstr- The characters to translate in the source. */
/* char *tostr - The corresponding characters the source */
/* characters are translated to. */
/* */
/* Return Value: */
/* The starting address of the source string. */
/* */
/************************************************************FUNCTION**/
char *
translate ( char *p_source, char *p_from_string, char *p_to_string )
{
unsigned x_sub = 0, l_len = strlen( p_to_string );
while ( *p_from_string ) {
if ( strchr( p_source, *p_from_string ) != NULL ) {
if ( x_sub < l_len )
chrsubst( p_source, *p_from_string, p_to_string[x_sub] );
else {
char l_ch[] = "";
l_ch[0] = *p_from_string;
str_rmchars( p_source, l_ch );
}
}
x_sub++;
p_from_string++;
}
return p_source;
}