Skip to main content

string.h

The string.h library is a library that works with C-strings. As strings are null terminated character arrays, and arrays are passed to functions by the addresses, some of the library's functions will alter the arrays given to them. We will focus our attention on 4 of the most commonly used string functions for discussions.

strcpy()

strcpy() is a function that that will copy one string to another. Every character up to and including the null terminating character is copied. It is the responsibility of the calling function to ensure that the destination string is big enough to hold the contents of the source string.


char destination[10];
//copies "hello" into destination
strcpy(destination, "hello");

//prints:
//hello
printf("%s\n", destination);

strcmp

If you have 2 strings you cannot compare them using standard mathematical operators. This is because the operators would simply compare their locations in memory which is typically not useful. Instead what you usually want is to compare the characters. To do this you need to use the strcmp() function.

The strcmp(s1,s2) function compares two strings s1 and s2. If the two strings contains exactly the same characters, the function returns 0. If the two are different, it will return it will return a negative number if the first mismatched character in s1 has a lower value than s2 as it indicates that s1 is lexically earlier. If the first mismatched character in s1 has a higher value than s2, the function would return a positive value.

For example, "apple" is lexically earlier than "banana" (if we were to list these two words in alphabetical order we would list it as "apple", "banana")

Thus:

strcmp("apple", "banana") would return a negative value.

and

strcmp("banana", "apple") would return a positive value.

Note that when checking results, always compare against 0.

Given 2 strings s1 and s2, this table gives you the strcmp expression you want to use. Note that test is purely conceptual.. if you write the expression in the test column into your program, it will not have the desired effect.

test you want to makecorrect expression using strcmp
s1 == s2strcmp(s1,s2) == 0
s1 != s2strcmp(s1, s2) != 0
s1 < s2strcmp (s1, s2) < 0
s1 > s2strcmp (s1, s2) > 0

strlen()

The strlen() function is passed a null terminated string and returns the number of character before the null terminating character.

For example strlen("abc") returns 3 because there are 3 characters before the terminating null character

One of the most important things to note that the null-terminating nature of C strings actually makes this function fairly slow. Unlike other languages where strings store/update the length, C strings do not. The only way for strlen() to find the number of characters is to scan for the terminating \0 char. The longer the string, the more time it takes to find this character. This makes how we call the strlen() function really important.

For example consider these two functions that counts the number of times the character ch appears in the string str. Version A is SIGNIFICANTLY Faster. Be very careful about using strlen() inside a loop as it will make your program slow significantly if you are not careful.

//version A: This function calls strlen() once before the loop
//and uses that to go through the string as an array
int countVersionA(char ch, char str[])
{
int count = 0;
int i;
int length = strlen(str);
for(i = 0;i<length;i++){
if(str[i] == ch){
count++;
}
}
return count;
}

//version B: This function calls strlen() in the loop
//condition. This version is SIGNIFICANTLY SLOWER
//because every check will require a call to strlen()
//which is very slow... the longer the string the slower
//it is. DO NOT DO THIS!
int countVersionB(char ch, char str[])
{
int count = 0;
int i;
for(i = 0;i<strlen(str);;i++){
if(str[i] == ch){
count++;
}
}
return count;
}

strcat()

strcat() function stands for string concatenate. This function accepts two strings and appends the content of the second string to the end of the first string.

char s1[20] = "hello";
char s2[20] = "world";

strcat(s1,s2);
//this will print:
//helloworld
printf("%s\n", s1);

Some of the C string.h Library Functions

This is not a complete list of C string.h library functions. Some of the string library functions should not be used as they have significant drawbacks.

FunctionPurposeExample
strlen(s)Returns length of s (excluding null terminator)strlen("hello") returns 5
strcpy(dest, src)Copies the contents of src into deststrcpy(s, "hello") makes s hold "hello"
strncpy(dest, src)Copies up to n characters of src into deststrncpy(s, "hello", 3) makes s hold "hel"
strcat(s1, s2)Concatenates s2 to s1strcat(dest, "World") where dest originally held "hello" makes dest hold "helloWorld"
strncat()Concatenates up to n characters from source to destinationstrncat(dest, "World", 3) where dest originally held "hello" makes dest hold "helloWor"
strcmp(s1, s2)Compares two strings; returns 0 if equal, negative is s1 < s2 (s1 is lexically earlier than s2) or positive is s1 > s2 (s1 is lexically later than s2)strcmp("abc", "abc") returns 0
strncmp()Compares up to n characters of two stringsstrncmp("hello", "help", 3) returns 0, strncmp("hello", "help", 4) returns a negative value
strchr()Finds first occurrence of character in stringstrchr("banana", 'a') returns pointer to first 'a' that follows the 'b'
strrchr()Finds last occurrence of character in stringstrrchr("banana", 'a') returns pointer to last 'a' (the one at the end of "banana")
strstr()Finds first occurrence of substring in stringstrstr("hello world", "world") returns pointer to "world"