C Strings: char Arrays, strlen, strcpy, strcmp
C has no separate string type. You put the characters one after another in a char array and place a \0 (the null character) at the very end to mark "it stops here". That's why you assign and compare strings with functions from <string.h>, not with operators.
char s[size] = "characters";
strlen(s) strcpy(dest, src) strcmp(a, b)
The char array and the end marker
char s[] = "cat"; makes an array of 4 elements: 3 characters plus the end marker \0. strlen counts the characters before the \0, and %s in printf also prints until it meets the \0.
#include <stdio.h>#include <string.h> int main(void) { char s[] = "cat"; printf("%s\n", s); printf("%d\n", (int)strlen(s)); printf("%d\n", (int)sizeof s); printf("%c %d\n", s[0], s[3]); return 0;}cat 3 4 c 0
The \0 in s[3] is the character whose code is 0. An array that holds a string always needs at least the number of characters + 1 elements.
Copying with strcpy and joining with strcat
You can't assign an array with =, so you use strcpy(dest, src) to move a string. strcat(first, second) appends the second string to the end of the first. Neither one checks that the receiving array is big enough.
#include <stdio.h>#include <string.h> int main(void) { char a[20] = "hello"; char b[20]; strcpy(b, a); b[0] = 'J'; strcat(b, "!"); printf("%s %s\n", a, b); printf("%d\n", (int)strlen(b)); return 0;}hello Jello! 6
After the copy they are two different arrays, so changing b leaves a as it was.
Comparing with strcmp
strcmp(a, b) returns 0 when the two strings are equal. It returns a negative number if a comes first in dictionary order and a positive number if it comes later. The exact value is not fixed, so only test it with == 0, < 0 or > 0.
#include <stdio.h>#include <string.h> int main(void) { char a[] = "apple", b[] = "banana", c[] = "apple"; if (strcmp(a, c) == 0) printf("same\n"); if (strcmp(a, b) < 0) printf("apple first\n"); if (strcmp(b, a) > 0) printf("banana later\n"); if (strcmp("Zoo", "apple") < 0) printf("Zoo first\n"); return 0;}same apple first banana later Zoo first
The comparison uses character codes. Uppercase letters have smaller codes than lowercase letters, so Zoo comes before apple.
Changing a string one character at a time
A string is an array, so you can read and change one character with s[i]. You can find the end without measuring the length, by checking s[i] != '\0'. toupper, which changes a letter to uppercase, is in <ctype.h>.
#include <stdio.h>#include <ctype.h> int main(void) { char s[] = "hello c world"; int spaces = 0; for (int i = 0; s[i] != '\0'; i++) { if (s[i] == ' ') spaces++; else s[i] = toupper((unsigned char)s[i]); } printf("%s\n", s); printf("%d\n", spaces); return 0;}HELLO C WORLD 2
Common mistakes
Comparing strings with ==
char a[] = "hi", b[] = "hi";
if (a == b) printf("same\n");
else printf("different\n");
→ The contents are the same, but you get different. The compiler may also warn you
== between two array names compares where the two arrays are (their addresses), not their characters. They are different arrays, so it is always false. Write strcmp(a, b) == 0.
Putting a string into an array with =
char name[20];
name = "Mina"; /* only allowed in the declaration */
→ It doesn't compile
You can use = only in the declaration, as in char name[20] = "Mina";. After that, use strcpy(name, "Mina");.
Not counting the element for \0
char s[5] = "hello"; /* 5 characters, no room for \0 */
printf("%s\n", s);
char t[5];
strcpy(t, "hello"); /* needs 6 elements */
→ The first line compiles, but only the characters are stored, with no \0. Printing an array that has no end marker, or copying 6 elements into a 5-element array, is undefined behavior
Give the array at least the number of characters in the longest string + 1 elements. If you leave the size empty, as in char s[] = "hello";, it gets 6 elements by itself.
Trying to change a string literal
char *s = "cat";
s[0] = 'b'; /* a literal can't be changed */
→ It compiles, but it is undefined behavior. The program often crashes while running
The string you write in double quotes sits in a place that must not be changed. For a string you want to change, make an array, as in char s[] = "cat";, so you have your own copy.