0

我试图弄清楚如何搜索输入到字符串数组中的名称列表。如果输入的名称是原始数组的一部分,则搜索函数应返回字符串在数组中的位置;如果未找到该字符串,则应返回 -1。如果返回-1,那么我希望能够打印出“未找到”,这似乎不太难弄清楚,但是如果找到名称,我希望能够打印出找到名称的位置。

这是我的代码,显然我是新手,所以我可能已经屠杀了应该如何完成。我的其余代码似乎工作正常,但正是这个功能让我不知所措。

#include<stdio.h>
#include<conio.h>
#include<string.h>

#define MAX_NAMELENGTH 10
#define MAX_NAMES 5
void initialize(char names[MAX_NAMES][MAX_NAMELENGTH]);
int search(char names[MAX_NAMES][MAX_NAMELENGTH],int i,Number_entrys);
int main()
{
    char names[MAX_NAMES][MAX_NAMELENGTH];
    int i;
    initialize(names);
    search(names,i,Number_entrys);
    search_result= search(names,i,Number_entrys);
    if (search_result==-1){
        printf("Found no names.\n");
    }
    if(search_result==0){
       printf("Name found");
    }    getch();
    return 0;
}

void initialize(char names[MAX_NAMES][MAX_NAMELENGTH])
{
    int i, Number_entrys;

    printf("How many names would you like to enter to the list?\n");
    scanf("%d",&Number_entrys);

    if(Number_entrys>MAX_NAMES){
               printf("Please choose a smaller entry\n");
    }else{
        for (i=0; i<Number_entrys;i++){
            scanf("%s",names[i]);
        }
    }
    for(i=0;i<Number_entrys;i++){

        printf("%s\n",names[i]); 
    }
}

int search(char names[MAX_NAMES][MAX_NAMELENGTH],int i)
{
    int j, idx;
    char name[MAX_NAMELENGTH];
    printf("Now enter a name in which you would like to search the list for:");
    scanf("%s", name);

    for(x = 0; x < Number_entrys; x++) {
        if ( strcmp( new_name, names[x] ) == 0 ){
            /* match, x is the index */
            return x;
        }else{
            return -1;   
        }
    }
}
4

3 回答 3

0

你的意思是这样的:

int search(char names[MAX_NAMES][MAX_NAMELENGTH], int i)
{
     int j, idx;
     char name[MAX_NAMELENGTH];
     printf("Now enter a name in which you would like to search the list for:");
     scanf("%s", name);

     idx = -1;
     for(j = 0; j < i; j++){
         if(strstr(names[i], name) != NULL){
             idx = j;break;
         }
     }
     return idx;
}
于 2012-10-28T02:42:33.423 回答
0

这里有几个问题。

搜索的目的是要求用户输入要搜索的单个名称。那么为什么是

 char new_name[MAX_NAMES][MAX_NAMELENGTH];

你只需要一个数组

 char new_name[MAX_NAMELENGTH];

然后你有一个循环,你只需要循环一次,所以你不需要循环

 scanf("%s",new_name);

就足够了。这感觉就像您复制了用于填充名称数组的代码,但还没有真正理解其本质。

另一个问题是您无法控制用户可以输入多长时间的名称。如果用户输入一个很长的名字会发生什么?你会过度填充数组,你的程序可能会崩溃和烧毁。阅读本文以了解如何控制它。

要真正迂腐,您还应该检查 scanf 的返回码,您希望读取一项,因此返回值应为 1,否则将是错误。

然后您尝试使用 strstr() 来查看 char 数组的数组。strstr文档说它的目的是在字符串中搜索子字符串,而不是搜索字符串数组。

因此,只需手动搜索数组

/* what is i? the number of items used in the array? */
for(x = 0; x < i; x++) {
    if ( strcmp( new_name, names[x] ) == 0 ){
        /* match, x is the index */
        return x;
    }
}
/* here with no match */
return -1;

在你的主要

int 搜索结果;

search_result = search( /* etc */ );

if ( search_result == -1 ) {
     /* print "not found" here */
} else {
     /* print something else */
}
于 2012-10-28T02:54:00.863 回答
0

(8) ...
也许我们会扭转
局面 因为现在还不算太晚
它永远不会太晚!!(8)
:) !


对不起这首歌^_^...哦,好吧,我在这个问题上玩了几分钟,我希望这段代码对你有更多帮助,关于:

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


/* return array of indexs */

int*
search(const char *list, size_t slen, const char **arr_names, size_t arrlen)
{
    int *arr_idx;
    int j,i,idx;

    i=idx=0;

    arr_idx = malloc(sizeof(int)*arrlen+1);

    if(!arr_idx)
        exit(EXIT_FAILURE);

    for(i=0; i<slen; i++) {
        for(j=0; j<arrlen ; j++) {
            if(!strncmp(&list[i], &arr_names[j][0], strlen(arr_names[j]))) {
                arr_idx[idx] = j;
                idx++;
            }
        }
    }

    arr_idx[idx] = -1; /* -1 terminated array */
    if(!idx) {
        free(arr_idx);
        return NULL; /* found no names */
    }

   return arr_idx;
}
/* I'm a sick [something], nul terminated strings :P */
const char *string   = "My favorite artists: ATB, Nujabes and Bonobo also Aphex Twins is nice, along with Trapt and Breaking Benjamin.\0";
const char *names[] = {"ATB\0", "Scifer\0", "Aphex\0", "Bonobo\0", "Nujabes\0", "Tipper\0"};
#define N_NAMES 6

int
main(void)
{
    int i;
    int *array;

    array = search(string, strlen(&string[0]), names, N_NAMES);

    if(!array) {
        puts("Found no names.\n");
        return EXIT_SUCCESS;
    }

    printf("Names found: ");
    for(i=0; array[i]!=-1; i++)
        printf("%s,", names[array[i]]);

    printf("\b \n");
    free(array);  /* important */
    /* system("pause"); */   /* NOTE: decomment this if you're on windows */
   return EXIT_SUCCESS;
}

进行了一些测试:
 
~$ ./program


输出
Names found: ATB,Nujabes,Bonobo,Aphex

于 2012-10-28T04:29:46.087 回答