欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

两头堵模型

程序员文章站 2022-07-14 18:34:19
...
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int get_count_non_space(char *str, int * count_p)
{
    int i = 0;
    int j = strlen(str)-1;
    int ncount = 0;

    if (str == NULL || count_p == NULL) {
        fprintf(stderr, "str == NULL, count_p == NULL\n");
        return -1;
    }
    while (isspace(str[i]) && str[i] != '\0')
    {
        i++;
    }

    while (isspace(str[j]) && j > i)
    {
        j--;
    }

    ncount = j-i+1;

    *count_p = ncount;

    return 0;
}

int main(void)
{
    char *p = "      abcdefg      ";

    int ret = 0;
    int ncount = 0;

    ret = get_count_non_space(p, &ncount);
    if (ret < 0) {
        fprintf(stderr, "get_count_non_space err, ret = %d\n", ret);
        return 0;
    }

    printf("ncount = %d\n", ncount);

    return 0;
}