Saturday, August 16, 2008

常见的字符串操作

这个月写了CDMA编解码,说实在的,就是和字符串打交道。总结一下,写在这里方便查阅。

1. 单个字符串替换
void
my_str_replace(char* buf, char from, char to)
{
int i = 0;
while (buf[i]) {
if (buf[i] == from)
buf[i] = to;

++i;
}
}


2. 过滤前后空格
char*
stripwhite (char *string)
{
register char *s, *t;

for (s = string; whitespace (*s); s++)
;

if (*s == 0)
return (s);

t = s + strlen (s) - 1;
while (t > s && whitespace (*t))
t--;

*++t = '\0';

return s;
}

to be continued...

No comments: