int find_char(const char*str1,const char*str2)这个函数第二个是字符串时无法输出
时间: 2023-09-19 11:05:49 浏览: 40
这个函数的目的是查找字符串str2中第一个在字符串str1中出现的字符,并返回该字符在字符串str1中的下标。如果第二个参数传递的是一个字符串,则只会查找该字符串的第一个字符。如果该字符不存在于字符串str1中,则该函数会返回-1。
如果您无法输出结果,可能是因为您没有正确地调用该函数或者没有正确地处理函数返回的值。请注意,该函数返回的是一个int类型的值,您需要使用printf等函数将其输出到控制台或者其他地方。以下是一个使用该函数的示例代码:
```c
#include <stdio.h>
int find_char(const char* str1, const char* str2);
int main() {
const char* str1 = "hello world";
const char* str2 = "ow";
int index = find_char(str1, str2);
if (index != -1) {
printf("第一个匹配的字符在字符串str1中的下标是%d\n", index);
} else {
printf("字符串str2中的字符在字符串str1中不存在\n");
}
return 0;
}
int find_char(const char* str1, const char* str2) {
for (int i = 0; str2[i] != '\0'; i++) {
for (int j = 0; str1[j] != '\0'; j++) {
if (str1[j] == str2[i]) {
return j;
}
}
}
return -1;
}
```
在上面的代码中,我们首先定义了两个字符串str1和str2,然后调用了find_char函数来查找字符串str2中第一个出现在字符串str1中的字符。如果该字符存在,则输出其在字符串str1中的下标;如果不存在,则输出一个提示信息。请注意,我们在main函数中使用了printf函数来输出结果。