From 955d347cc62ddea22f980e7b691f3b6f3133646c Mon Sep 17 00:00:00 2001 From: Deinerovych Date: Thu, 26 Sep 2024 23:24:26 +0200 Subject: [PATCH] 4 --- cv1/program.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index 254af3e..79897a6 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -38,24 +38,23 @@ int contains_substring(char *food_name, char *ingredient) { int main() { char ingredient[100]; - char food_name[100]; + char food_name[200]; char price[20]; int count = 0; - printf("Zadaj hladanu surovinu: "); - scanf("%s", ingredient); + printf("Zadaj hladanu surovinu:\n"); + if (!fgets(ingredient, sizeof(ingredient), stdin)) { + return 1; + } + ingredient[strcspn(ingredient, "\n")] = '\0'; printf("Zadaj jedalny listok:\n"); while (fgets(food_name, sizeof(food_name), stdin)) { - if (food_name[strlen(food_name) - 1] == '\n') { - food_name[strlen(food_name) - 1] = '\0'; - } + food_name[strcspn(food_name, "\n")] = '\0'; if (!fgets(price, sizeof(price), stdin)) { break; } - if (price[strlen(price) - 1] == '\n') { - price[strlen(price) - 1] = '\0'; - } + price[strcspn(price, "\n")] = '\0'; if (contains_substring(food_name, ingredient)) { printf("%s\n%s\n", food_name, price);