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];

必要な配列は 1 つだけです

 char new_name[MAX_NAMELENGTH];

次に、1回だけループするループがあるので、ループは必要ありません

 scanf("%s",new_name);

で十分でしょう。これは、名前の配列を埋めるために使用したコードをコピーしたように感じますが、その本質を本当に理解していません.

もう 1 つの問題は、ユーザーが入力する名前の長さを制御できないことです。ユーザーが非常に長い名前を入力するとどうなりますか? 配列がいっぱいになると、プログラムがクラッシュして燃えてしまう可能性があります。これを制御する方法については、この記事をお読みください。

本当にペダンティックにするには、scanf からの戻りコードも確認する必要があります。1 つの項目を読み取ることを期待しているため、戻り値は 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_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 に答える