[英]String array searching in C
我試圖弄清楚如何搜索輸入到字符串數組中的名稱列表。 如果輸入的名稱是原始數組的一部分,則搜索功能應返回字符串在數組中的位置;否則,返回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;
}
}
}
您的意思是這樣的:
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;
}
這里有幾個問題。
搜索的目的是要求用戶輸入要搜索的單個名稱。 那為什么
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_result = search( /* etc */ );
if ( search_result == -1 ) {
/* print "not found" here */
} else {
/* print something else */
}
(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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.