Skip to content

Commit ff36537

Browse files
authored
Optimized strcat.c
In case s wasn't big enough for whatever reason, the function would still work with this optimized version. Also, for the purpose of compactness, comparing with the null-character can be omitted, e.g "if (x != '\0')" is the same as "if (x != 0)" which is the same as "if (x)".
1 parent 7f3097e commit ff36537

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

chapter_5/exercise_5_03/strcat.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ int main(void)
1818
void strcat_ptr(char *s, char *t)
1919
{
2020
// Find the end of s
21-
while ((*++s) != '\0')
22-
;
21+
while (*s)
22+
++s;
2323

2424
// copy t to the end of s
25-
while ((*s++ = *t++) != '\0')
25+
while (*s++ = *t++)
2626
;
2727
}

0 commit comments

Comments
 (0)