C语言常用的两头堵模型

来源:互联网 发布:知乎怎么收费回答问题 编辑:程序博客网 时间:2024/04/27 23:21

两头堵模型是开发中常用的模型,用于去掉字符串两头的空格

#include <stdio.h>#include <string.h>#include <stdlib.h>#include <ctype.h>int strimSpace(char* bufin ,char* bufout){    int i = 0;    int j =0;    int count = 0;//定义需要复制的字符串长度    if(bufin == NULL||bufout == NULL)    {        perror("error:bufin or bufout is NULL");        return -1;    }     j = strlen(bufin)-1;//将j定位在末尾    while(isspace(bufin[i])&&bufin[i]!='\0')//从头开始处理    {        i++;    }    while(isspace(bufin[j])&&bufin[j]!='\0')//从结尾处理    {        j--;    }    count = j-i+1;//得到应该复制字符串的长度    strncpy(bufout,bufin+i,count);    return 0;}int main(){    char * a = " hello";    char  b[20] = {0};        strimSpace(a,b);    printf("strlen(b)=%d,%s\n",strlen(b),b);    return 0;}

哈哈,又学一招,不知道有没有bug呢?疑问就算有以后再调....

原创粉丝点击