Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中字符串的字符数组中搜索字符串 char names[MAX_CLASS_SIZE][MAX_NAME_SIZE+1]={“朱利亚斯·霍夫曼”、“黛安·康纳”、“米切尔·库珀”、“约翰尼·格林”、“约翰娜·梅森”、“凯文·阿德金斯”、“布兰迪·斯宾塞”、“玛丽安·泰勒”、“切斯特·克罗斯”、“马丁·劳伦斯”、“简·史密斯”、“莎拉·琼斯”}; char specificName[]=“”; int搜索(字符名称[][最大名称大小+1],字符特定名称[]) for(i=0;i_C_Arrays_String_Search_Char - Fatal编程技术网

在C中字符串的字符数组中搜索字符串 char names[MAX_CLASS_SIZE][MAX_NAME_SIZE+1]={“朱利亚斯·霍夫曼”、“黛安·康纳”、“米切尔·库珀”、“约翰尼·格林”、“约翰娜·梅森”、“凯文·阿德金斯”、“布兰迪·斯宾塞”、“玛丽安·泰勒”、“切斯特·克罗斯”、“马丁·劳伦斯”、“简·史密斯”、“莎拉·琼斯”}; char specificName[]=“”; int搜索(字符名称[][最大名称大小+1],字符特定名称[]) for(i=0;i

在C中字符串的字符数组中搜索字符串 char names[MAX_CLASS_SIZE][MAX_NAME_SIZE+1]={“朱利亚斯·霍夫曼”、“黛安·康纳”、“米切尔·库珀”、“约翰尼·格林”、“约翰娜·梅森”、“凯文·阿德金斯”、“布兰迪·斯宾塞”、“玛丽安·泰勒”、“切斯特·克罗斯”、“马丁·劳伦斯”、“简·史密斯”、“莎拉·琼斯”}; char specificName[]=“”; int搜索(字符名称[][最大名称大小+1],字符特定名称[]) for(i=0;i,c,arrays,string,search,char,C,Arrays,String,Search,Char,快速查看了一下,它似乎工作得很好。您的i需要声明为int,否则,以下工作正常: char names [MAX_CLASS_SIZE][MAX_NAME_SIZE+1]={"Julias Hoffman","Dianne Conner","Mitchell Cooper","Johnnie Greene","Johanna Mason","Kevin Adkins","Brandi Spencer","Marian Tyler","Chester Cross","Martin Lawrence"

快速查看了一下,它似乎工作得很好。您的i需要声明为int,否则,以下工作正常:

char names [MAX_CLASS_SIZE][MAX_NAME_SIZE+1]={"Julias Hoffman","Dianne Conner","Mitchell Cooper","Johnnie Greene","Johanna Mason","Kevin Adkins","Brandi Spencer","Marian Tyler","Chester Cross","Martin Lawrence","Jane Smith","Sara Jones"};

char specificName[]="";

int search(char names[][MAX_NAME_SIZE+1],char specificName[])

for(i=0;i<MAX_CLASS_SIZE;i++){
    if (strcmp(names[i],specificName)==0)
    {
        printf("Found %s",names[i]);
        return i;
    }
}
#包括
#包括
#定义最大类尺寸100
#定义最大名称大小100
int strcmp(常量字符*,常量字符*);
int main(int argc,const char*argv[]{
字符名称[MAX_CLASS_SIZE][MAX_NAME_SIZE+1]={“朱丽亚斯·霍夫曼”、“黛安·康纳”、“米切尔·库珀”、“约翰尼·格林”、“约翰娜·梅森”、“凯文·阿德金斯”、“布兰迪·斯宾塞”、“玛丽安·泰勒”、“切斯特·克罗斯”、“马丁·劳伦斯”、“简·史密斯”、“莎拉·琼斯”};
char specificName[]=“布兰迪·斯宾塞”;
int搜索(字符名称[][MAX_NAME_SIZE+1],字符特定名称[]);

对于(int i=0;i您可以尝试以下方法:

#include <stdio.h>
#include <stdlib.h>

#define MAX_CLASS_SIZE 100
#define MAX_NAME_SIZE 100

int strcmp(const char *, const char *);
int main(int argc, const char * argv[]) {

    char names [MAX_CLASS_SIZE][MAX_NAME_SIZE+1]={"Julias Hoffman","Dianne Conner","Mitchell Cooper","Johnnie Greene","Johanna Mason","Kevin Adkins","Brandi Spencer","Marian Tyler","Chester Cross","Martin Lawrence","Jane Smith","Sara Jones"};
    char specificName[]="Brandi Spencer";
    int search(char names[][MAX_NAME_SIZE+1],char specificName[]);
    for(int i=0;i<MAX_CLASS_SIZE;i++){
        if (strcmp(names[i],specificName)==0){
            printf("Found %s",names[i]);
            return i;
        }
    }

    return 0;
}

请解释,但它似乎工作不正常。您遇到了什么错误?或者出现了什么意外结果?以及如何将输入存储到
specificName
?很抱歉,不清楚。函数找不到匹配的名称。我正在使用scanf(“%s”,specificName)存储输入;@MichaelSciortino请检查我的答案。非常感谢。这种方法已经奏效,我相信我意识到,正是我存储输入的方式给了我意外的输出。我如何存储输入,以便它同时存储名字和姓氏?因为有必要,我向用户请求输入。@MichaelSciortino:)请稍等。@MichaelSciortino请现在查看我的答案。问题是fgets存储了
'\n'
(换行符)这里需要一个类似于
strcspn
的函数来消除它。谢谢你,亚历山大,非常感谢。你说的输入没有正确传递是对的。我该如何存储输入,以便它同时存储名字和姓氏?因为这是必要的,我要求用户输入。你可以使用strok()函数拆分字符串(并指定要使用的分隔符,即“”),如果要将其缩小一点,我可能希望使用字典之类的东西作为存储和/或实现链表(使其更易于删除)。希望这对您有所帮助。
#include <stdio.h>
#include <stdlib.h>
#include<string.h>

int search(char **names,char *specificName, size_t length){
    size_t i,found=0;
    for(i=0;i<length;i++){
        if (strcmp(*(names + i),specificName)==0){
            found = 1;
        }
    }
    return found == 1 ? 0 : 1;
}


int main(void) {
    size_t length;
    char specificName[100];
    char *names[12]={"Julias Hoffman","Dianne Conner","Mitchell Cooper","Johnnie Greene",
                     "Johanna Mason","Kevin Adkins","Brandi Spencer","Marian Tyler",
                     "Chester Cross","Martin Lawrence","Jane Smith","Sara Jones"};

    length = sizeof names / sizeof *(names + 0);

    printf("Type a name to be searched:> ");
    if( fgets (specificName, 100, stdin) == NULL){
        printf("Error!\n");
    }

    specificName[strcspn(specificName, "\n")] = 0;

    if((search(names,specificName,length)) == 0){
        printf("Found %s",specificName);
    }else{
        printf("There was no Record Found.\n");
    }
    return 0;
}
Type a name to be searched:> Sara Jones
Found Sara Jones