昨天代码中用到一个小功能,去掉字符串中最右侧的空格,今天闲的无聊,就索性把去掉左侧空格,和去掉字符串中全部空格的方法都写一下吧。
方法一,使用两个变量:
#include <stdio.h>
#include <string.h>
int Strim(char *strDst, char *strSrc) {
int i = 0;
int y = 0;
memset(strDst, '\0', sizeof(strDst));
while(strSrc[i] != '\0') {
if (strSrc[i] == ' ') {
i++;
continue;
}
strDst[y] = strSrc[i];
i++;
y++;
}
return 0;
}
int trim(char *strDst, char *strSrc) {
int i = 0;
int j = 0;
j = strlen(strSrc) - 1;
while(strSrc[i] == ' ') {
++i;
}
while(strSrc[j] == ' ') {
--j;
}
strncpy(strDst, strSrc + i, j - i + 1);
strDst[j - i + 1] = '\0';
return 0;
}
int Ltrim(char *strDst, char *strSrc) {
int i =