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

【注:本程序验证是使用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; }

 

posted @ 2019-06-29 07:00  大黄蜂_001  阅读(202)  评论(0编辑  收藏  举报