Skip to content

Commit

Permalink
Merge pull request #69 from sadeem-albir/patch-10
Browse files Browse the repository at this point in the history
Update strncmp.c
  • Loading branch information
ohkimur authored Apr 1, 2024
2 parents 5daa9d8 + 60fe24d commit 7a45948
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions chapter_5/exercise_5_05/strncmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ int main(void)
// Return <0 if s<t, 0 if s==t, >0 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;
Expand All @@ -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 is holding, so the final result will be 0 - t_ascii_value.

return *s - *t;
}

0 comments on commit 7a45948

Please sign in to comment.