如何在另一个字符串中正确找到匹配字符

发布于 2025-02-07 19:49:17 字数 655 浏览 1 评论 0原文

我想知道将字符串的单个字符与另一个整个字符串进行比较的最佳方法,可以说可以找到匹配的字符。我代码中最重要的片段是用户输入请求: 字符串text = get_string(“ text:”); 和我预定义的“比较” -String = char lowerabc [] =“ abcdefghijklmnopqrstuvwxyz”; 现在,我想采用文本的n个值,并在LowerAbc中找到匹配的字符。 我的第一次尝试是使用for-loop,并将终端标准设置为当值匹配时,像这样的smth:for(int j = 0; text [i] == lowerabc [j]; j ++){ } 但是,正如我发现的那样,它比较了两者的地址,因此没有任何意义。因此,我偶然发现了strcmp()函数,并编辑了代码以匹配零值作为结束标准:for(int j = 0; strcmp; strcmp(text [i],lowerabc [j])== 0; j ++){} ,但是一旦我对其进行了编译,它就会给我带来错误:不兼容的整数,以指针转换传递“ char”到类型'const char *'的参数;使用& [-werror,-wint-conversion]对不起,但似乎我太固执了,无法确定这两个变量的性质差异,以便对我这样说Smth。因此,我想问一下,进行这种类型的比较的最有效方法是什么。非常感谢您的帮助

i would like to know the best way to compare a single character of a string to another whole string and find the matching character so to say. The probably most important snippets of my code are the user input request:
string text = get_string("text: ");
and my predefined "comparison"-string= char lowerabc[] = "abcdefghijklmnopqrstuvwxyz";
Now, i would like to take the n-th value of the text and find the matching character in lowerabc.
My first try was to use a for-loop and set the end-criteria to when their values match, smth like this: for(int j=0;text[i] == lowerabc[j] ; j++){}
but as i've found out this compares the address of both and therefore makes no sense. Therefore i stumbled upon the strcmp() function and edited the code to match the zero-value as the end criteria: for(int j=0;strcmp(text[i], lowerabc[j])==0 ; j++){} but once i compile it, it throws me the error: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with & [-Werror,-Wint-conversion] Im sorry, but it really seems like that im too stubborn to make out the difference in nature of these two variables so that it would say smth like that to me. So therefore i would like to ask what would be the most efficient way to do this type of comparison. Ty all very much for your help

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

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

发布评论

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

评论(1

救星 2025-02-14 19:49:17

您可以使用 strchr() 从字符串中找到单个字符。

#include <stdio.h>
#include <string.h>

/* mock */
typedef char* string;
string get_string(const char* x) {
    static char ret[] = "hoge123abc";
    return ret;
}

int main(void) {
    string text = get_string("text:  ");
    char lowerabc[] = "abcdefghijklmnopqrstuvwxyz";

    for (int i = 0; text[i] != '\0'; i++) {
        char* res = strchr(lowerabc, text[i]);
        if (res != NULL) {
            int idx = res - lowerabc;
            printf("%c is found at lowerabc[%d]\n", text[i], idx);
        } else {
            printf("%c is not found in lowerabc\n", text[i]);
        }
    }
    return 0;
}

输出:

h is found at lowerabc[7]
o is found at lowerabc[14]
g is found at lowerabc[6]
e is found at lowerabc[4]
1 is not found in lowerabc
2 is not found in lowerabc
3 is not found in lowerabc
a is found at lowerabc[0]
b is found at lowerabc[1]
c is found at lowerabc[2]

You can use strchr() to find single character from a string.

#include <stdio.h>
#include <string.h>

/* mock */
typedef char* string;
string get_string(const char* x) {
    static char ret[] = "hoge123abc";
    return ret;
}

int main(void) {
    string text = get_string("text:  ");
    char lowerabc[] = "abcdefghijklmnopqrstuvwxyz";

    for (int i = 0; text[i] != '\0'; i++) {
        char* res = strchr(lowerabc, text[i]);
        if (res != NULL) {
            int idx = res - lowerabc;
            printf("%c is found at lowerabc[%d]\n", text[i], idx);
        } else {
            printf("%c is not found in lowerabc\n", text[i]);
        }
    }
    return 0;
}

output:

h is found at lowerabc[7]
o is found at lowerabc[14]
g is found at lowerabc[6]
e is found at lowerabc[4]
1 is not found in lowerabc
2 is not found in lowerabc
3 is not found in lowerabc
a is found at lowerabc[0]
b is found at lowerabc[1]
c is found at lowerabc[2]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文