From 3eb785a320fcff95ddb05e73e759365a52f77c04 Mon Sep 17 00:00:00 2001 From: Yevhen Kozirovskyi Date: Tue, 1 Oct 2024 22:00:31 +0000 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20cv1/program.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cv1/program.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index e627e03..92918f2 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -1,8 +1,10 @@ #include #include #include + #define MAX_DISHES 100 #define MAX_NAME_LEN 100 + void decodovane_slovo(char *text) { for (int i = 0; text[i]; i++) { switch (text[i]) { @@ -20,17 +22,17 @@ void decodovane_slovo(char *text) { } } } + int main() { char ingredient[MAX_NAME_LEN]; char dishes[MAX_DISHES][2][MAX_NAME_LEN]; int count = 0; - printf("Zadaj hladanu surovinu: "); fgets(ingredient, MAX_NAME_LEN, stdin); ingredient[strcspn(ingredient, "\n")] = '\0'; - printf("Zadaj jedalny listok: \n"); + printf("Zadaj jedalny listok:\n"); while (1) { char name[MAX_NAME_LEN], price[MAX_NAME_LEN]; @@ -40,12 +42,18 @@ int main() { if (strlen(name) == 0) { break; } + // Ввод цены fgets(price, MAX_NAME_LEN, stdin); price[strcspn(price, "\n")] = '\0'; - strcpy(dishes[count][0], name); - strcpy(dishes[count][1], price); + // Использование strncpy для безопасности + strncpy(dishes[count][0], name, MAX_NAME_LEN - 1); + dishes[count][0][MAX_NAME_LEN - 1] = '\0'; // Обеспечение завершенности строки + + strncpy(dishes[count][1], price, MAX_NAME_LEN - 1); + dishes[count][1][MAX_NAME_LEN - 1] = '\0'; // Обеспечение завершенности строки + count++; } @@ -53,15 +61,15 @@ int main() { for (int i = 0; i < count; i++) { char decoded_name[MAX_NAME_LEN]; strncpy(decoded_name, dishes[i][0], MAX_NAME_LEN - 1); - decoded_name[MAX_NAME_LEN - 1] = '\0'; + decoded_name[MAX_NAME_LEN - 1] = '\0'; // Гарантия завершения строки decodovane_slovo(decoded_name); if (strstr(decoded_name, ingredient)) { - printf("%s\n%s\n", dishes[i][0], dishes[i][1] ); + printf("%s\n%s\n", dishes[i][0], dishes[i][1]); matches++; } } - printf("%s%d%s","Nacitanych ",count," poloziek."); + printf("%s%d%s", "Nacitanych ", count, " poloziek.\n"); return 0; -} \ No newline at end of file +}