如何在 C 语言中找到字符串中字符的索引?

发布于 2024-09-09 05:18:54 字数 167 浏览 2 评论 0原文

假设我有一个字符串 "qwerty" 并且我希望找到其中 e 字符的索引位置。 (在这种情况下,索引将为2

我如何在 C 中做到这一点?

我找到了 strchr 函数,但它返回一个指向字符的指针,而不是索引。

Suppose I have a string "qwerty" and I wish to find the index position of the e character in it. (In this case the index would be 2)

How do I do it in C?

I found the strchr function but it returns a pointer to a character and not the index.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

爱,才寂寞 2024-09-16 05:18:55

您还可以使用 strcspn(string, "e") 但这可能会慢得多,因为它能够处理搜索多个可能的字符。使用strchr并减去指针是最好的方法。

You can also use strcspn(string, "e") but this may be much slower since it's able to handle searching for multiple possible characters. Using strchr and subtracting the pointer is the best way.

很快妥协 2024-09-16 05:18:55
void myFunc(char* str, char c)
{
    char* ptr;
    int index;

    ptr = strchr(str, c);
    if (ptr == NULL)
    {
        printf("Character not found\n");
        return;
    }

    index = ptr - str;

    printf("The index is %d\n", index);
    ASSERT(str[index] == c);  // Verify that the character at index is the one we want.
}

该代码目前未经测试,但它演示了正确的概念。

void myFunc(char* str, char c)
{
    char* ptr;
    int index;

    ptr = strchr(str, c);
    if (ptr == NULL)
    {
        printf("Character not found\n");
        return;
    }

    index = ptr - str;

    printf("The index is %d\n", index);
    ASSERT(str[index] == c);  // Verify that the character at index is the one we want.
}

This code is currently untested, but it demonstrates the proper concept.

罪歌 2024-09-16 05:18:55

这应该可以做到:

//Returns the index of the first occurence of char c in char* string. If not found -1 is returned.
int get_index(char* string, char c) {
    char *e = strchr(string, c);
    if (e == NULL) {
        return -1;
    }
    return (int)(e - string);
}

This should do it:

//Returns the index of the first occurence of char c in char* string. If not found -1 is returned.
int get_index(char* string, char c) {
    char *e = strchr(string, c);
    if (e == NULL) {
        return -1;
    }
    return (int)(e - string);
}
习ぎ惯性依靠 2024-09-16 05:18:55

怎么样:

char *string = "qwerty";
char *e = string;
int idx = 0;
while (*e++ != 'e') idx++;

复制到 e 以保留原始字符串,我想如果你不关心你可以只对 *string 进行操作

What about:

char *string = "qwerty";
char *e = string;
int idx = 0;
while (*e++ != 'e') idx++;

copying to e to preserve the original string, I suppose if you don't care you could just operate over *string

℡Ms空城旧梦 2024-09-16 05:18:54

只需从 strchr 返回的内容中减去字符串地址:

char *string = "qwerty";
char *e;
int index;

e = strchr(string, 'e');
index = (int)(e - string);

请注意,结果是从零开始的,因此在上面的示例中它将是 2。

Just subtract the string address from what strchr returns:

char *string = "qwerty";
char *e;
int index;

e = strchr(string, 'e');
index = (int)(e - string);

Note that the result is zero based, so in above example it will be 2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文