gpt4 book ai didi

c - 从字符串数组中搜索字符串

转载 作者:太空宇宙 更新时间:2023-11-04 01:53:27 25 4
gpt4 key购买 nike

<分区>

我创建了一个接受字符串并将它们存储在二维数组中的程序,它还有一个在数组中搜索字符串(由用户输入)的函数。但是每当我运行该程序时,它都会在到达搜索部分时停止工作。谁能指出我的错误?

这是我的代码

#include <stdio.h>
#include <string.h>
#define maxname 40
#define maxlength 70

int acceptSize()
{
int sizeOf;
printf("How many students?");
scanf("%d",&sizeOf);
return sizeOf;
}

void acceptNames(char names[maxname][maxlength],int size)
{
int ctr;
for(ctr=0; ctr<size; ctr++)
{
printf("Student %d:", ctr+1);
scanf("%s",&names[ctr]);
}

}

int searchName(char names[maxname][maxlength], char sname[maxname]){
int ctr2;
for(ctr2=0; ctr2<maxname; ctr2++)
{
if(strcmp(names[ctr2], sname[maxname])==0)
{
return ctr2;
}
}
return -1;
}

int main(){
int ctr,size, choice, result;
char names[maxname][maxlength], sname[maxlength];

size=acceptSize();
acceptNames(names,size);
printf("Enter name to be searched: ");
scanf("%s", &sname[maxlength]);
result=searchName(names, sname);
if(result == -1)
printf("Match is not found.");
else
printf("Match is found!");
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com