Because strings are arrays, C cannot copy or compare them with = and ==. The standard library header <string.h> supplies functions for these jobs. This section covers the functions that belong to standard C; the next covers the non-standard ones mentioned in the study material.
The four core functions
Function
Purpose
Returns
strlen(s)
Number of characters before '\0'
A size_t (print with %zu)
strcpy(dest, src)
Copies src, including '\0', into dest
dest
strcat(dest, src)
Appends src to the end of dest
dest
strcmp(s1, s2)
Compares two strings character by character
Negative, zero or positive
strlen does not count the '\0': strlen("TYRAN") is 5, although the string occupies 6 bytes. For strcpy and strcat, the destination array must be large enough for the result plus '\0'; the functions do not check.
How strcmp compares
Comparison of this kind is called lexicographic order. Because it uses character codes, all capital letters come before all small letters in ASCII (‘Z’ is 90, ‘a’ is 97), so strcmp("Zebra", "apple") is negative.
strspn and the other standard functions
strspn(s1, s2) returns the length of the initial part of s1 that consists only of characters found anywhere in s2. s2 is treated as a set of allowed characters, not as a string to match position by position.
Function
Purpose
Example
Result
strncpy(d, s, n)
Copies at most n characters
strncpy(d, "HELLO", 3)
d gets H, E, L (no '\0' added)
strncat(d, s, n)
Appends at most n characters, then '\0'
strncat(d, "WORLD", 2)
Appends “WO”
strncmp(a, b, n)
Compares at most n characters
strncmp("apple", "apply", 4)
0
strchr(s, c)
Address of the first c in s, or NULL
strchr("programming", 'g')
Points to “gramming”
strstr(s, t)
Address of the first t in s, or NULL
strstr("programming", "ram")
Points to “ramming”
Key takeaways
Include <string.h> for the string functions.
strlen excludes '\0'; strcpy copies; strcat appends without adding a space.
strcmp returns 0 for equal, negative if the first is smaller, positive if greater.
strspn counts leading characters of the first string that belong to the set in the second.
Read input with fgets, never gets.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Write a fragment that reads a password with fgets and prints “Access granted” only if it equals “mca2026”. Explain the condition used.
Show worked answer
char pw[30]; fgets(pw, sizeof pw, stdin); pw[strcspn(pw, "\n")] = '\0'; if (strcmp(pw, "mca2026") == 0) printf("Access granted\n"); else printf("Access denied\n");. fgets reads the line safely, and the newline is removed so that it does not take part in the comparison. strcmp returns 0 only when every character matches and both strings end together, so == 0 is the test for equality; using pw == "mca2026" would compare addresses, not contents.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/arrays-and-strings/standard-string-functions/ to read this page.