两头堵模型

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(void){
    char *p = "      abcdefg      ";
    int begin = 0;
    int end = strlen(p) - 1;  //数组下标从0开始,所以要减-1
    int n = 0;

    //从左边开始
    //如果当前字符为空,而且没有结束
    while (p[begin] == ' ' && p[begin] != 0)
    {
        begin++; //位置从右移动一位
    }

    //如果当前字符为空,而且没有结束
    while (p[end] == ' ' && p[end] != 0)
    {
        end--; //往左移动
    }

    n = end - begin + 1;
    printf("n = %d\n", n);

    printf("\n");
    system("pause");
    return 0;


}

猜你喜欢

转载自blog.csdn.net/u013988442/article/details/84102243