- 论坛徽章:
- 0
|
这是我测试的程序在dev-c++编译通过。。
#include <stdio.h>
#include <string.h>
#include <memory.h>
void trim1( char *str )
{
char *copied, *tail = NULL;
if ( str == NULL )
return;
for( copied = str; *str; str++ )
{
if ( *str != ' ' && *str != '\t' )
{
*copied++ = *str;
tail = copied;
}
else
{
if ( tail )
*copied++ = *str;
}
}
if ( tail )
*tail = 0;
else
*copied = 0;
return;
}
void trim2(char *str)
{
char *be, *end, *p;
int i = 1;
if(str == NULL)
return;
be = end = NULL;
for(p = str;*p;p++)
{
if(*p != ' ' && *p != '\t')
{
if(i == 1)
{
be = p; // begin
}
end = p; // end
i++;
}
}
memcpy(str,be,end-be+1);
*(str+(end-be+1)) = 0;
return;
}
int main()
{
char buff[50];
char buff1[50];
char *p = " AAAA BB ";
strcpy(buff,p);
strcpy(buff1,p);
printf("before buffer = [%s]\n",buff);
trim1(buff);
printf("after buffer = [%s]\n",buff);
printf("before buffer1 = [%s]\n",buff1);
trim2(buff1);
printf("after buffer1 = [%s]\n",buff1);
return 0;
} |
|