代码: 全选
#include <stdio.h>
#include <string.h>
void trimr(char s[], unsigned int i);
void trimr(char s[], unsigned int l)
{
int length = strlen(s);
int i;
if(length >= l)
s[length - l] = '\0';
if( l == 0 )
{
for ( i = length - 1; i >= 0; i--)
{
printf("%d\n",i);
if(s[i] != ' ' || s[i] != '\t')
break;
}
s[i + 1] = '\0';
}
}
int main()
{
char s[] = "012345 6789 ";
printf("%s abcde\n",s);
trimr(s, 0);
printf("%s abcde\n",s);
return 0;
}
另外,当把数组写成
代码: 全选
char *s = "xxxxx"
代码: 全选
s[length - l] = '\0';