首页 > 其他 > 详细

2.33模型--去除字符串两头空格.c

时间:2019-06-29 09:09:06      阅读:92      评论:0      收藏:0      [点我收藏+]

【注:本程序验证是使用vs2013版】

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable:4996)

int my_strRidSpace(char *dest,char *src, int *n){
    char *buf = src;
    int begin = 0;
    int end = strlen(buf) - 1;
    int len = 0;

    if (buf == NULL || dest==NULL){ return -1; }
    //从左边开始
    while (buf[begin] ==   && buf[begin] != 0){    //如果当前字符不为空,而且没有结束
        begin++;//位置 右移动一位
    }
    while (buf[end] ==   && buf[end] != 0){    //如果当前字符不为空,而且没有结束
        end--;//位置 左移动一位
    }
    len = end - begin + 1;
    *n = len;
    strncpy(dest, buf + begin, len);//strncpy():拷贝字符串到dest,指定长度(后边不添加0)
    dest[len] = 0;
    /*这里是自己实现的指定长度拷贝字符串函数
    for (int i = 0; i < len; i++){
        *dest = *(buf + begin);
        dest++;
        buf++;
    }
    */

    return 0;
}


int main(void){
/* 查找非空格字符串个长度,两头堵模型   例子:" asfqwfq " */
char *p = " asdqwfrd "; char str[50] = { 0 }; int n = 0; int ret = 0; ret = my_strRidSpace(str,p, &n); if (ret != 0){ printf("my_strRidSpace err %d", ret); return ret; } printf("str = %s\n", str); printf("n= %d\n", n);
  技术分享图片 printf(
"\n"); system("pause"); return 0; }

 

2.33模型--去除字符串两头空格.c

原文:https://www.cnblogs.com/wlstm/p/11105500.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!