如何找出字符串的大小写样式(pascal,snake,kebab,camel)

lf5gs5x2  于 2023-04-29  发布在  其他
关注(0)|答案(1)|浏览(125)

我想写一个程序,输出一个字符串是用pascal,camel,snake,kebab,还是没有。在第一行中,我们应该从input中获取整数n。然后,在接下来的n行中,我们应该取一个整数k,然后每行取一个没有空格的字符串。k是字符串的大小。下面是这些大小写样式示例:

pascal case => HelloWorld
camel case => helloWorld
snake case => hello_world
kebab case => hello-world

例如:输入:

3
11 masterShifu
12 master_shifu
12 MASTER_SHIFU

输出:
Camel 箱
蛇管套
未定义大小写样式
这个程序的问题是我不知道如何找到每个单词的第一个字母,因为两个不同的单词之间没有空格。这是我到目前为止写的:

#include <stdio.h>
#include <ctype.h>

int
main()
{
    int n;

    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        int k;

        scanf("%d ", &k);
        char word[k];

        gets(word[k]);
        int count = 0;

        if (isupper(word[0]) == 1) {
            for (int i = 1; i < n; i++) {
                if (isupper(word[i]) == 1) {
                    count++;
                }
            }
            if (count == k)
                printf("Undefined Case Style\n");
            else
                printf("PascalCase\n");
        }
        else {
            int c = 0,
                s = 0,
                count = 0;

            for (int i = 0; i < n; i++) {
                if (word[i] == '-') {
                    c++;
                    printf("kebab-case\n");
                    break;
                }
                else if (word[i] == '_')    // snake case
                {
                    s++;
                    printf("snake_case\n");
                    break;
                }
                else if (isupper(word[i]) == 1) // camel case
                {
                    count++;
                    printf("camelCase\n");
                    break;
                }

            }
            if (c == 0 && s == 0)
                printf("Undefined Case Style\n");
        }
    }
    return 0;
}
hivapdat

hivapdat1#

我建议你遍历输入字符串s,然后给定当前字符*s和你找到的found的大小写,确定*s是否有效,新的found是什么,或者你可以终止处理。

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>

enum str_case {
    CAMEL,
    KEBAB,
    PASCAL,
    SNAKE,
    UNDEFINED,
};

enum str_case str2str_case(const char *s) {
    enum str_case found = isupper(*s++) ? PASCAL : UNDEFINED;
    for(; *s; s++) {
        if(*s == '-') {
            if(found == KEBAB)
                continue;
            if(found == UNDEFINED) {
                found = KEBAB;
                continue;
            }
            return UNDEFINED;
        }
        if(*s == '_') {
            if(found == SNAKE)
                continue;
            if(found == UNDEFINED) {
                found = SNAKE;
                continue;
            }
            return UNDEFINED;
        }
        if(isupper(*s)) {
            if(found == CAMEL || found == PASCAL)
                continue;
            if(found == UNDEFINED) {
                found = CAMEL;
                continue;
            }
            return UNDEFINED;
        }
        if(!islower(*s))
            return UNDEFINED;
    }
    return found;
}

int main(void) {
    struct test {
        char *input;
        enum str_case expected;
    } tests[] = {
        {"helloWorld", CAMEL},
        {"hello-world", KEBAB},
        {"HelloWorld", PASCAL},
        {"hello_world", SNAKE},
        {"", UNDEFINED},
        {"!", UNDEFINED},
        {"A-b", UNDEFINED},
        {"a_b-c", UNDEFINED},
    };
    for(struct test *t = tests; t < tests + sizeof tests / sizeof *tests; t++) {
        enum str_case got = str2str_case(t->input);
        if(t->expected != got) {
            printf("fail: %s: expected %d but got %d\n", t->input, t->expected, got);
            continue;
        }
        printf("ok\n");
    }
}

这里是输出:

ok
ok
ok
ok
ok
ok
ok
ok

然后我会用宏重构代码:

#define CHECK(char_predicate, state_predicate, new_state) \
    if((char_predicate)) {\
        if((state_predicate))\
            continue;\
        if(found == UNDEFINED) {\
            found = (new_state);\
            continue;\
        }\
        return UNDEFINED;\
    }

函数就变成了:

enum str_case str2str_case(const char *s) {
    enum str_case found = isupper(*s++) ? PASCAL : UNDEFINED;
    for(; *s; s++) {
        CHECK(*s == '-', found == KEBAB, KEBAB);
        CHECK(*s == '_', found == SNAKE, SNAKE);
        CHECK(isupper(*s), found == CAMEL || found == PASCAL, CAMEL);
        if(!islower(*s))
            return UNDEFINED;
    }
    return found;
}

除了使用fgets()而不是gets()之外,您似乎没有遇到解析输入格式和生成预期输出格式的问题,因此将其作为练习留给读者。

相关问题