Search function

This commit is contained in:
Win 2024-09-01 18:25:50 +07:00
parent 854b282a0e
commit 8811598366
2 changed files with 29 additions and 8 deletions

Binary file not shown.

View File

@ -21,7 +21,7 @@ struct vendor_item
} vendors[MAX_VENDORS];
int read_input(char str[], int n);
int search(struct vendor_item vendors[MAX_VENDORS], char query[]);
int search(char query[], struct vendor_item vendors[MAX_VENDORS], int total_vendors);
int main()
{
@ -87,7 +87,15 @@ int main()
char query[100];
read_input(query, 100);
search(vendors, query);
int query_length = strlen(query);
for (int i = 0; i < query_length; i++)
{
query[i] = tolower(query[i]);
};
search(query, vendors, total_vendors);
fclose(fp);
free(file_buffer);
@ -110,14 +118,27 @@ int read_input(char str[], int n)
return i;
};
int search(struct vendor_item vendors[MAX_VENDORS], char query[])
int search(char query[], struct vendor_item vendors[MAX_VENDORS], int total_vendors)
{
int query_length = strlen(query);
for (int i = 0; i < query_length; i++)
int i;
for (i = 0; i < total_vendors; i++)
{
query[i] = tolower(query[i]);
if (strcmp(query, vendors[i].merchandise) == 0)
{
printf("Query: %s | Result: %s %s\n",
query,
vendors[i].vendor_name,
vendors[i].merchandise
);
break;
};
printf("Query: %s\n", query);
};
if (i == total_vendors)
{
printf("No results found - %d\n", i);
}
return 0;
};