From 40937ff6fd1648f68535c3eae5a381541dcf5372 Mon Sep 17 00:00:00 2001 From: sadeem_albir <142955101+sadeem-albir@users.noreply.github.com> Date: Fri, 22 Mar 2024 08:04:02 +0300 Subject: [PATCH 1/2] Update strncmp.c Increased the accuracy of the comment description, and removed the " != '\0' " part of the while test. Comparing two characters for equality doesn't require seeing if their comparison is not equal to null, it is the same as writing "while ((x == y) != 0)". If x and y are ints or any other datetype, the '==' check suffices when checking for a boolean value. --- chapter_5/exercise_5_05/strncmp.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/chapter_5/exercise_5_05/strncmp.c b/chapter_5/exercise_5_05/strncmp.c index 5505781..a0933c6 100644 --- a/chapter_5/exercise_5_05/strncmp.c +++ b/chapter_5/exercise_5_05/strncmp.c @@ -29,7 +29,7 @@ int main(void) // Return <0 if s0 if s>t *1 int strcmp_ptr(char *s, char *t, size_t n) { - while ((*s == *t) != '\0' && --n) + while ((*s == *t) && --n) { if (*s == '\0') return 0; @@ -39,12 +39,12 @@ int strcmp_ptr(char *s, char *t, size_t n) } // If the s string contains more characters than t, then the t char will - // become '\0' before s char. If this happen then the s char will be >0 and - // t char will be 0, so the final result will be >0. + // become '\0' before s char. If this happen then the s char will be its ascii value and + // t char will be 0, so the final result will be s_ascii_value - 0. // If the t string contains more character than s, then the s char will - // become '\0' before t char. If this happen then the s char will be <0 and - // t char will be 0, so the final result will be <0. + // become '\0' before t char. If this happen then the s char will be 0 and + // t char will be whatever ascii_value it's holding, so the final result will be 0 - t_ascii_value. return *s - *t; } From 60fe24df3db5af0a9703b0e754ade74a76f3393d Mon Sep 17 00:00:00 2001 From: Daniel Costrasel Date: Mon, 1 Apr 2024 14:16:33 +0200 Subject: [PATCH 2/2] Update strncmp.c --- chapter_5/exercise_5_05/strncmp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapter_5/exercise_5_05/strncmp.c b/chapter_5/exercise_5_05/strncmp.c index a0933c6..fe90bd1 100644 --- a/chapter_5/exercise_5_05/strncmp.c +++ b/chapter_5/exercise_5_05/strncmp.c @@ -44,7 +44,7 @@ int strcmp_ptr(char *s, char *t, size_t n) // If the t string contains more character than s, then the s char will // become '\0' before t char. If this happen then the s char will be 0 and - // t char will be whatever ascii_value it's holding, so the final result will be 0 - t_ascii_value. + // t char will be whatever ascii_value is holding, so the final result will be 0 - t_ascii_value. return *s - *t; }