Skip to content

Commit 7a45948

Browse files
authored
Merge pull request #69 from sadeem-albir/patch-10
Update strncmp.c
2 parents 5daa9d8 + 60fe24d commit 7a45948

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

chapter_5/exercise_5_05/strncmp.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ int main(void)
2929
// Return <0 if s<t, 0 if s==t, >0 if s>t *1
3030
int strcmp_ptr(char *s, char *t, size_t n)
3131
{
32-
while ((*s == *t) != '\0' && --n)
32+
while ((*s == *t) && --n)
3333
{
3434
if (*s == '\0')
3535
return 0;
@@ -39,12 +39,12 @@ int strcmp_ptr(char *s, char *t, size_t n)
3939
}
4040

4141
// If the s string contains more characters than t, then the t char will
42-
// become '\0' before s char. If this happen then the s char will be >0 and
43-
// t char will be 0, so the final result will be >0.
42+
// become '\0' before s char. If this happen then the s char will be its ascii value and
43+
// t char will be 0, so the final result will be s_ascii_value - 0.
4444

4545
// If the t string contains more character than s, then the s char will
46-
// become '\0' before t char. If this happen then the s char will be <0 and
47-
// t char will be 0, so the final result will be <0.
46+
// become '\0' before t char. If this happen then the s char will be 0 and
47+
// t char will be whatever ascii_value is holding, so the final result will be 0 - t_ascii_value.
4848

4949
return *s - *t;
5050
}

0 commit comments

Comments
 (0)