file: trim.c
点击(此处)折叠或打开
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #ifndef SUCCESS
- #define SUCCESS 1
- #endif
- /* TRIM LEFT */
- int rtrim( char *pszString)
- {
- int nForwardCursor = 0;
- int nTrailingCursor = 0;
- int bTrim = 1;
- for( nForwardCursor = 0 ; pszString[nForwardCursor] != '\0'; nForwardCursor++ )
- if ( bTrim && isspace( pszString[nForwardCursor] ))
- continue ;
- else {
- bTrim = 0;
- pszString[nTrailingCursor] = pszString[nForwardCursor];
- nTrailingCursor++;
- }
- pszString[nTrailingCursor] = '\0';
- return SUCCESS;
- }
- /* TRIM RIGHT */
- int ltrim( char *pszString )
- {
- int nForwardCursor = 0;
- int nTrailingCursor = 0;
- int bTrim = 1;
- for ( nForwardCursor=strlen(pszString)-1;
- nForwardCursor >= 0 && isspace( pszString[nForwardCursor] );
- nForwardCursor-- )
- {
- }
- pszString[nForwardCursor+1] = '\0';
- return SUCCESS;
- }
- /* TRIM LEFT & RIGHT */
- int trim(char *str)
- {
- ltrim(str);
- rtrim(str);
- return SUCCESS;
- }
file: test.c
点击(此处)折叠或打开
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- int main( void )
- {
- char s[80];
- sprintf(s,"%s"," aaaaaa ");
- printf("=== strlen = %d === \n",strlen(s));
- trim(s);
- printf("=== len = %d ===\n",strlen(s));
- return 0;
- }
cc -o test test.c trim.c
运行:
$ ./test