added functions

This commit is contained in:
Win 2024-09-06 08:52:53 +07:00
parent 2944612155
commit 1f5d50221a
2 changed files with 23 additions and 8 deletions

Binary file not shown.

View File

@ -38,7 +38,9 @@ struct purchase_r
};
/* FUNCTION DECLARATION */
int read_input(char str[], int n);
int read_input(char str[], size_t n);
char *strnlower(char *s, size_t n);
int validate_data(struct vendor vendors[MAX_VENDORS]);
struct search_r search
(
@ -129,11 +131,8 @@ int main()
// convert string to lowercase - each character one by one
int user_query_length = strlen(user_query);
for (int i = 0; i < user_query_length; i++)
{
user_query[i] = tolower(user_query[i]);
};
strnlower(user_query, user_query_length);
struct search_r item = search(user_query, vendors, total_vendors);
if(item.status == 0)
{
@ -177,13 +176,13 @@ int main()
reads one character at a time into buffer and adds a null terminator when finish reading
*/
int read_input(char str[], int n)
int read_input(char str[], size_t n)
{
int ch, i = 0;
while ((ch = getchar()) != '\n')
{
if (i < n)
if (i < (int)n)
{
str[i++] = ch;
}
@ -193,6 +192,22 @@ int read_input(char str[], int n)
return i;
};
/*
strnlower: Convert string to lowercase
Parameters:
- s: String
- n: Length of string
*/
char *strnlower(char *s, size_t n)
{
for (int i = 0; i < (int) n; i++)
{
s[i] = tolower(s[i]);
};
}
/*
Search function