1 Star 0 Fork 1

sdbzmyh/learnC2

forked from guohuacai/learnC2 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
c语言实现去除字符串左右两边的空格.txt 1.67 KB
一键复制 编辑 原始数据 按行查看 历史
guohuacai 提交于 2021-01-29 07:20 . 学习总结
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int trimstr(char* dst,char *src)
{
char* from = src;
char* to = dst;
if (dst == NULL || src == NULL)
{
return -1;
}
int begin = 0;
int end = strlen(from) - 1;
while (from[begin]==' '&& from[begin]!=0)
{
begin++;
}
while (from[end] == ' '&& from[end] != 0)
{
end--;
}
int count = end - begin + 1;
src = src + begin;
strncpy(dst, src, count);
dst[count] = '\0';
return 0;
}
int trimstr2(char* dst, char *src)
{
char* from = src;
char* to = dst;
int i = 0;
int j = 0;
if (dst == NULL || src == NULL)
{
return -1;
}
int begin = 0;
int end = strlen(from) - 1;
while (from[begin] == ' '&& from[begin] != 0)
{
begin++;
}
while (from[end] == ' '&& from[end] != 0)
{
end--;
}
int count = end - begin + 1;
printf("src=%s", src);
for ( i = 0,j=begin; i < count,j<=end; i++,j++)
{
dst[i] = src[j];
}
dst[count] = '\0';
return 0;
}
char * trim2ends(char* src){
char *dst = (char*)malloc(100);
int count = 0;
if (src != NULL)
{
char * from = src;
int begin = 0;
int end = strlen(from) - 1;
while (from[begin] == ' '&& from[begin] != 0)
{
begin++;
}
while (from[end] == ' '&& from[end] != 0)
{
end--;
}
count = end - begin + 1;
src = src + begin;
strncpy(dst, src, count);
}
dst[count] = 0;
return dst;
}
int main(int argc, char const *argv[])
{
char* p = " i love sexy girls ";
//char dst[100];
char *result;
//trimstr2(dst,p);
result = trim2ends(p);
printf("result=%s\n", result);
system("pause");
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sdbzmyh/learn-c2.git
git@gitee.com:sdbzmyh/learn-c2.git
sdbzmyh
learn-c2
learnC2
master

搜索帮助