如何在另一个字符串中正确找到匹配字符
我想知道将字符串的单个字符与另一个整个字符串进行比较的最佳方法,可以说可以找到匹配的字符。我代码中最重要的片段是用户输入请求: 字符串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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
strchr()
从字符串中找到单个字符。输出:
You can use
strchr()
to find single character from a string.output: