- 论坛徽章:
- 0
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *trim(char *_cp_ptr)
{
int count;
char *cp_head;
char *cp_mark;
char *cp_tail;
if ( NULL == _cp_ptr )
return(NULL);
// "\0" (ASCII 0 (0x00)),空字节符
// "\t" (ASCII 9 (0x09)),制表符
// "\n" (ASCII 10 (0x0A)),换行符
// "\r" (ASCII 13 (0x0D)),回车符
// " " (ASCII 32 (0x20)),普通空格符
// "\x0B" (ASCII 11 (0x0B)),垂直制表符
cp_head = _cp_ptr;
while ( ( *cp_head != '\0' ) && ( *cp_head == ' ' || *cp_head == '\n' || *cp_head == '\r' || *cp_head == '\v' || *cp_head == '\t' ) )
cp_head++;
for ( cp_tail = cp_mark = cp_head; *cp_mark != '\0'; cp_mark++ )
{
if ( *cp_mark == ' ' || *cp_mark == '\n' || *cp_mark == '\r' || *cp_mark == '\v' || *cp_mark == '\t' )
continue;
cp_tail = cp_mark;
}
count = (cp_tail - cp_head) + 1;
memmove(_cp_ptr, cp_head, count);
*(_cp_ptr + count) = '\0';
return(_cp_ptr);
}
int main(int argc, char *argv[])
{
char ca_string[] = " \n \n \r \v \t 123 456 789 \n \r \v \t ";
printf("ca_string = %s", ca_string);
printf("\n -- -- -- -- -- -- -- -- \n");
printf("trim(ca_string) = %s", trim(ca_string));
return(0);
} |
|