简体   繁体   English

在C中返回并打印字符串数组索引

[英]Returning and printing string array index in C

I've got a function that searches through a list of names and I'm trying to get the search function to return the index of the array back to the main function and print out the starting location of the name found. 我有一个搜索名称列表的函数,并且试图获取搜索函数以将数组的索引返回到主函数并打印出找到的名称的起始位置。 Everything I've tried up to this point either crashes the program or results in strange output. 到目前为止,我尝试过的所有操作都使程序崩溃或导致奇怪的输出。

Here is my search function: 这是我的搜索功能:

#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 Number_entrys, int i);
int search(char names[MAX_NAMES][MAX_NAMELENGTH], int Number_entrys);

int main()
{
   char names[MAX_NAMES][MAX_NAMELENGTH];
   int i, Number_entrys,search_result,x;
   printf("How many names would you like to enter to the list?\n");
   scanf("%d",&Number_entrys);
   initialize(names,Number_entrys,i);
   search_result= search(names,Number_entrys);
   if (search_result==-1){
      printf("Found no names.\n");
   }else
   {
      printf("%s",search_result);
   }
   getch();
   return 0;
}

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

int search(char names[MAX_NAMES][MAX_NAMELENGTH],int Number_entrys)
{
   int x;
   char new_name[MAX_NAMELENGTH];
   printf("Now enter a name in which you would like to search the list for\n");
   scanf("%s",new_name);

   for(x = 0; x < Number_entrys; x++) {
      if ( strcmp( new_name, names[x] ) == 0 )
      {
         return x;
      }
   } 
   return -1;        
}

Like I mentioned before I have tried a lot of different ways to try and fix this issue, but I cant seem to get them to work. 就像我之前提到的那样,我尝试了许多不同的方法来尝试解决此问题,但是我似乎无法使它们起作用。 Printing X like what I have above is just the last thing I tried, and therefor know that it doesn't work. 像上面一样打印X只是我尝试的最后一件事,因此知道它不起作用。 Any suggestions on the simplest way to do this? 关于最简单的方法有什么建议吗?

Why don't you use strcmp instead of strstr ? 为什么不使用strcmp而不是strstr呢?

In your code it seems that there is some huge issues : -it seems that i is use not initialize. 在您的代码中,似乎存在一些巨大的问题:-似乎我使用的不是初始化。 -You declare x as a int and then use : printf("%s",x) it's kind of a non-sense here. -您将x声明为int,然后使用:printf(“%s”,x)在这里有点废话。 And by the way not initiliaze ! 顺便说一句不要初始化!

Something like that should be better (Do note that I haven't your initialize function) and I haven't try to compile : 这样的东西应该更好(请注意,我没有您的initialize函数),并且我没有尝试编译:

int search(char names[MAX_NAMES][MAX_NAMELENGTH],int Number_entrys)
{
   int x =0;
   char new_name[MAX_NAMELENGTH];
   printf("Now enter a name in which you would like to search the list for\n");
   scanf("%s",new_name);

   for(x = 0; x < Number_entrys; x++) 
   {
      if ( strcmp( new_name, names[x] ) == 0 )
      {
         return x;
      }
   } 
   return -1;        
 }

MAIN : 主要:

int main()
{
    char names[MAX_NAMES][MAX_NAMELENGTH];
    int i=0;
    int Number_entrys=0;
    int search_result=0;
    printf("How many names would you like to enter to the list?\n");
    scanf("%d",&Number_entrys);
    initialize(names,Number_entrys,i); // I guess it is use to initialize names ?!?
    search_result= search(names,Number_entrys);
    if (search_result==-1)
    {
         printf("Found no names.\n");
    }
    else
    {
       printf("Index found in position %d in the tab\n",search_result);
    }
   getch(); //not really a fan of this...
   return 0;
 }

Hope it helps. 希望能帮助到你。

Regards, 问候,

Joze 约兹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM