It would be extremely handy if the C Standard Library provided routines to trim whitespace both before and after a string. As we have seen, some of the input routines preserve whitespace, including <newline>, and some of the routines do not. Other languages provide functions such as trimLeft(), trimRight(), and trim(), that trim both the left and right sides of a string.
Thankfully, writing such a function is not too cumbersome in C. Consider the following function:
int trimStr( char* pStr ) {
size_t first , last , lenIn , lenOut ;
first = last = lenIn = lenOut = 0;
lenIn = strlen( pString ); //
char tmpString[ lenIn+1 ]; // Create working copy.
strcpy( tmpStr , pString );//
char* pTmp = tmpStr; // pTmp may change in Left Trim segment
// Left Trim
// Find 1st non-whitespace char; pStr will point to that.
while( isspace( pTmp[ first ] ) )
first++;
pTmp += first;
lenOut = strlen( pTmp ); // Get new length after Left Trim.
if( lenOut...