String Functions
<string.h> provides strlen, strcpy, strncpy, strcat, strcmp, strchr and strstr. Strings end with a zero byte \0, so buffers must be big enough.
Example
char a[20] = "Hello";
strcat(a, ", C");
printf("%s %zu\n", a, strlen(a));
printf("%d %d\n", strcmp("a", "b") < 0, strcmp("x", "x") == 0);
printf("%s\n", strstr(a, "C"));
char b[10]; strncpy(b, "abcdef", 3); b[3] = 0;
printf("%s\n", b);Result
Hello, C 8 1 1 C abc
Exercise
Compare strings.
("a", "b")Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.