Skip to content

Instantly share code, notes, and snippets.

@froop
Created May 9, 2011 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save froop/962699 to your computer and use it in GitHub Desktop.
Save froop/962699 to your computer and use it in GitHub Desktop.
[C言語] 文字列trim
char *c_trim(char *cpStr)
{
int i;
char *cpStart; /* 文字列の先頭位置 */
/* 後ろのスペースを削除 */
for (i = strlen(cpStr)-1; i >= 0; i--)
{
if (cpStr[i] != ' ')
{
break;
}
cpStr[i] = '\0';
}
/* 前スペースを除いた先頭を探す */
cpStart = cpStr;
while (*cpStart == ' ')
{
cpStart++;
}
/* 前スペース分をシフト */
memmove(cpStr, cpStart, strlen(cpStart) + 1);
return cpStr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment