Hi,
Here is an extension of MSDN's trim right example to trim left-right...
First subroutines then the calling structure is given below...
Cheers.
Ali Riza SARAL
//************************************************** ***********
char *trim_right( char *szSource )
{
char *pszEOS = 0;
// Set pointer to character before terminating NULL
pszEOS = szSource + strlen( szSource ) - 1;
// iterate backwards until non '_' is found
while( (pszEOS >= szSource) && (*pszEOS == '_') )
*pszEOS-- = '\0';
return szSource;
}
char *trim_left( char *szSource )
{
char *pszBOS = 0;
// Set pointer to character before terminating NULL
//pszEOS = szSource + strlen( szSource ) - 1;
pszBOS = szSource;
// iterate backwards until non '_' is found
while(*pszBOS == '_')
*pszBOS++;
return pszBOS;
}
char *trim( char *szSource )
{
return trim_left(trim_right(trim_left(szSource)));
//************************************************** ********************
char *trim_right( char *szSource );
char *trim_left( char *szSource );
char *trim( char *szSource );
//...
int main(int argc, char *argv[])
{
//...
char szbuf[] = "__12345_____";
printf_s("\nBefore trim: %s\n\n", szbuf);
//printf_s("\nAfter trim right: %s\n", trim_right(szbuf));
//printf_s("\nAfter trim left: %s\n", trim_left(szbuf));
printf_s("\nAfter trim: %s\n", trim(szbuf));
Note: Once again I have built this on MSDN's simple trim example. I kept their var names etc but completed the gap they have left kindly.