I'm trying to search a string array of names and return an integer to the main depending on if the search was successful or not. for example if the user entered name didn't exist it would return -1 and I can tell the user the name is not listed, but if the user enters a name that is listed it will return the postion of the string in the array. I'm pretty sure I need to use strcmp and maybe strcpy, but i've had a hard time understanding those completely in the context of what I'm trying to do with this program. Here is my code, Maybe you seasoned vets could help a rookie like myself out.

#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);
int main()
{



    char names[MAX_NAMES][MAX_NAMELENGTH];
    int i;
     initialize(names);
     search(names,i);
    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)
{
    char new_name[MAX_NAMES][MAX_NAMELENGTH];
    printf("Now enter a name in which you would like to search the list for");
    for(i=0;i<1;i++){
    scanf("%s",new_name[i]);
}
}

strstr() does exactly what you want, returning a pointer to the first char of the target string it finds. If the target string isn't found, strstr() returns NULL.

Wasn't that easy? ;)

if(strstr(string1, targetString)) {
   //code to handle a target string that was found
}else {
   //code to handle where a target string was not found
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.