#include<stdio.h> int find_char(const char*str1,const char*str2)//在字符串str1中找字符串str2的函数 { const char*p1=str1;//定义一个指针指向str1 const char*p2=str2;//定义一个指针指向str2 int pos=-1;//定义个位置变量,存str2在str1的位置 for(p1=str1;*p1!='\0';p1++) { if(*p1==*p2) { if(pos==-1) { pos=p1-str1; } p2++; if(*p2=='\0') { return pos; } else { p2=str2; if(pos!=-1) { p1=str1+pos; } pos=-1; } } } return -1; } int main() { char str1[100],str2[100]; printf("Input str1:\n"); gets(str1); printf("Input str2:\n"); gets(str2); printf("position=%d\n",find_char(str1,str2)); return 0; }代码为什么错了
时间: 2024-01-28 07:03:26 浏览: 23
在C语言中,函数声明需要放在程序开头,否则编译器可能无法识别函数。可以将 `find_char` 函数的声明放在 `main` 函数前面,如下所示:
```
#include<stdio.h>
int find_char(const char* str1,const char* str2);
int main() {
// 程序主体
}
int find_char(const char* str1,const char* str2) {
// 函数体
}
```
此外,使用 `gets` 函数读取字符串容易导致缓冲区溢出,因此建议使用 `fgets` 函数代替,例如:
```
fgets(str1, 100, stdin);
fgets(str2, 100, stdin);
```
相关问题
int find_char(const char*str1,const char*str2)这个函数第二个是字符串时无法输出
这个函数的目的是查找字符串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函数来输出结果。
#include<stdio.h> int find_char(const charstr1,const charstr2)//在字符串str1中找字符串str2的函数 { const charp1=str1;//定义一个指针指向str1 const charp2=str2;//定义一个指针指向str2 int pos=-1;//定义个位置变量,存str2在str1的位置 for(p1=str1;*p1!='\0';p1++) { if(*p1==*p2) { if(pos==-1) { pos=p1-str1; } p2++; if(*p2=='\0') { return pos; } else { p2=str2; if(pos!=-1) { p1=str1+pos; } pos=-1; } } } return -1; } int main() { char str1[100],str2[100]; printf("Input str1:\n"); gets(str1); printf("Input str2:\n"); gets(str2); printf("position=%d\n",find_char(str1,str2)); return 0; }第二个输入字符串,没有正确判断位置
您的代码存在一个错误,应该将函数声明的参数改为const char *类型,而不是const char类型。此外,您定义指针变量p1和p2时没有指定类型,应该将它们的类型声明为const char*,否则会产生警告或错误。
另外,您在函数内部使用了一个pos变量来记录字符串str2在字符串str1中的位置。但是,如果在查找过程中发现了多个匹配的位置,您只记录了第一个匹配的位置,后面的匹配位置会覆盖掉前面的位置。因此,您需要使用一个数组来记录所有匹配的位置。
最后,您的代码使用了gets函数来读取用户输入的字符串,但是这个函数可能会导致缓冲区溢出的问题,因此建议使用更安全的fgets函数来代替。以下是修改后的代码:
```c
#include <stdio.h>
#include <string.h>
int find_char(const char *str1, const char *str2) {
const char *p1 = str1;
const char *p2 = str2;
int pos[100]; // 保存所有匹配的位置
int count = 0; // 匹配次数
for (; *p1 != '\0'; p1++) {
if (*p1 == *p2) {
if (count == 0) {
pos[count++] = p1 - str1;
}
p2++;
if (*p2 == '\0') {
return pos[0];
} else {
p2 = str2;
if (count > 0) {
p1 = str1 + pos[count - 1];
}
pos[count++] = p1 - str1;
}
}
}
return -1;
}
int main() {
char str1[100], str2[100];
printf("Input str1:\n");
fgets(str1, sizeof(str1), stdin);
str1[strlen(str1) - 1] = '\0'; // 去掉fgets函数读入的换行符
printf("Input str2:\n");
fgets(str2, sizeof(str2), stdin);
str2[strlen(str2) - 1] = '\0';
int position = find_char(str1, str2);
if (position != -1) {
printf("position=%d\n", position);
} else {
printf("str2 not found in str1.\n");
}
return 0;
}
```
在上面的代码中,我们使用了fgets函数来读取用户输入的字符串,并且在读取后去掉了换行符。在find_char函数中,我们使用一个数组pos来保存所有匹配的位置,并使用count变量来记录匹配次数。如果在查找过程中发现了匹配,则将该位置保存到数组中,并将count加1。最后,我们根据pos数组中的第一个位置来判断字符串str2在字符串str1中的位置。