Skip to content

Commit 556cfe5

Browse files
committed
Refactor strcmp function for improved readability and structure
1 parent 44c485b commit 556cfe5

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

user/string.c

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,22 @@
1010
*/
1111
int strcmp(const char *str_1, const char *str_2)
1212
{
13-
unsigned char str1, str2;
14-
while(1)
15-
{
16-
str1 = *str_1++;
17-
str2 = *str_2++;
18-
19-
if (str1 != str2)
20-
{
21-
// NOTE: If needed in the future, can return the difference between
22-
// the strings rather than just -1 or 1
23-
return str1 < str2 ? -1 : 1;
24-
}
13+
unsigned char ch1, ch2;
14+
do
15+
{
16+
ch1 = *str_1++;
17+
ch2 = *str_2++;
2518

26-
if (!str1)
27-
{
28-
break;
29-
}
19+
if (ch1 != ch2)
20+
{
21+
// NOTE: If needed in the future, can return the difference between
22+
// the strings rather than just -1 or 1
23+
return ch1 < ch2 ? -1 : 1;
3024
}
3125

32-
return 0;
26+
} while (ch1);
27+
28+
return 0;
3329
}
3430

3531
/**

0 commit comments

Comments
 (0)