==================================================== ==============================
void trim(const char * orig, char * dest)
{
size_t front = 0;
size_t end = sizeof(orig) - 1;
size_t counter = 0;
char * tmp = null;
if (sizeof(orig) > 0)
{
memset(dest, '\0', sizeof(dest));
/* Find the first non-space character */
while (isspace(orig[front]))
{
front++;
}
/* Find the last non-space character */
while (isspace(orig[end]))
{
end--;
}
tmp = strndup(orig + front, end - front + 1);
strncpy(dest, tmp, sizeof(dest) - 1);
free(tmp); //strndup automatically malloc space
}
}
==================================================== ==============================
我有一个字符串:
'ABCDEF/G01'
上面的函数应该删除空格并返回给我:
'ABCDEF/G01'。
相反,我得到的是:
'ABCDEF/'
有任何想法吗?
注意:引号只是为了告诉您原始字符串中存在空格。