I'm trying to make a search string function, it will search a string if it's found it will return 1 or 0 if it's not found.
Code:
#include <stdio.h>
#include <string.h>
/*
A program for searching a string
*/
int stringSearch(char **str, char *key, int *position, int size);
int main()
{
char *str[5] ={"World", "Hello", "arcs", "Sober", "Fun"};
char *key;
int pos;
printf("Input the keyword:\n");
scanf("%s", key);
if(stringSearch(str, key, &pos,5))
{
printf("Found in element: %d of the array\n", pos);
}
else
{
printf("Not Found\n");
}
return 0;
}
int stringSearch(char *str[], char *key, int *position, int size)
{
int pos;
for (pos=0;pos<size ;pos++)
if (strcmp(str[pos],key)==0 )
{
*position = pos;
return 1;
}
return 0;
}
But the problem is the code always return 0, no matter what the input is. Anyone can help me?