APS105 Textbook 10.3 - String Functions

String Library

Since working with strings in C is horrible, we can use the string.h library to simplify our lives a bit. To do this, add #include <string.h> to the top of your file.


Length of String: strlen

The function strlen returns the length of a string.

size_t strlen(char *str);
Note: size_t is an unsigned integer type.

For the purposes of this course, the council has decided that we can treat size_t and int as the same thing.

Example of strlen

#include <stdio.h>
#include <string.h>
int main(void) {
	char s[] = "hello";
	printf("Size of \"hello\" is: %ld\n", strlen(s));
	return 0;
}

Equivalent of strlen without using <string.h>

If you need to get the length of a string without using the <string.h> library, you can use the following implementation:

#include <stdio.h>

int stringLength(const char* str) {
	int count = 0;
	while (str[count] != '\0') {
		++count;
	}
	return count;
}

int main(void) {
	char s[] = "hello";
	printf("Size of \"hello\" is: %d\n", stringLength(s));
	return 0;
}


Copying a String to Another String: strcpy

Using <string.h>, the function to copy a string to another string is strcpy, and its function prototype is as follows:

char* strcpy(char *dest, char *src);

This copies the source string to the destination string until it reaches \0 in the source string.

Example of strcpy

#include <stdio.h>
#include <string.h>

int main(void) {
	char src[] = "source string";
	char dest[13] = "po";
	strcpy(dest, src);
	printf("dest string after copying: %s\n", dest);
}

Equivalent of strcpy without using <string.h>

If you need to copy a string without using the <string.h> library, you can use the following implementation:

#include <stdio.h>

void stringCopy(char *dest, const char *src) {
	// note that the implementation in the textbook is shorter
	// i'm just too lazy to fix my code cuz it works anyways
	int sizeSrc = 0;
	while (src[sizeSrc] != '\0') {
		++sizeSrc;
	}

	for (int i = 0; i < sizeSrc; ++i) {
		dest[i] = src[i];
	}
	dest[sizeSrc] = '\0';
}

int main(void) {
	char src[] = "source string";
	char dest[13];
	stringCopy(dest, src);
	printf("dest string after copying: %s\n", dest);
}

Copy the first n characters of a string: strncpy

If you want to copy the first n characters of a string to a destination string, you can use strncpy if you have #include <string.h>. The syntax is as follows:

char* strncpy(char *dest, const char *src, size_t n);
Warning

This produces errors if the destination string is not large enough to hold the source string (up until the nth character).

Warning

strncpy doesn't add a null byte to the end of a destination string

Example of strncpy

#include <stdio.h>
#include <string.h>
int main(void) {
	char src[] = "source";
	char dest[7];
	int n = 4;
	strncpy(dest, src, n);
	printf("dest string after copying 6 characters: %s\n", dest);
}

Note that this produces sour as an output, which is not ideal. This is because strncpy does not add a null character to the end of your string.


Concatenating Strings: strcat and strncat

"Concatenate" means to add something to the end. In this case, strcat from the <string.h> library adds a source string to the end of a destination string, assuming the destination string is big enough to hold the concatenated string. The function prototype is as follows:

char* strcat(char *dest, const char *src);

This is not ideal because it concatenates the entire string. So, if the destination string isn't large enough, it produces an error. Thus, we can use strncat to only concatenate the first n characters.

char* strncat(char *dest, const char *src, size_t n);
Warning

strncat doesn't add a null byte to the end of a destination string

Example of strncat

#include <stdio.h>
#include <string.h>
int main(void) {
	char src[] = "source";
	char destb[10] = "dest";
	strncat(destb, src, 5);
	printf("string concatenated with strncat (n = 5): %s\n", destb);
}

Comparing Strings: strcmp or strncmp

strcmp in the <string.h> library iterates through two strings until it reaches a character that is different. Then, it outputs the ASCII difference between the first different characters in the two inputted strings. This can either be negative or positive. The syntax is as follows:

char *strcmp(const char *s1, const char *s2);

The function returns:

strcmp assumes that the strings are terminated by \0

If the function doesn't find a different character, it will compare characters until it reaches the \0. If the strings are terminated by a \0, then undefined behaviour will occur (it will compare random characters in memory)

If you only want to compare the first n characters in each string, you can instead use strncmp, which has the following syntax:

char *strncmp(const char *s1, const char *s2, size_t n);

Example of strcmp and strncmp

#include <stdio.h>
#include <string.h>
int main(void) {
	char str1[] = "smoge";
	char str2[] = "smoke";
	printf("strcmp produces: %d\n", strcmp(str1, str2));
	printf("strncmp (n = 3) produces: %d\n", strncmp(str1, str2, 3));
	return 0;
}

Searching for Character in String: strchr

strchr in the <string.h> library returns a pointer to the first index of a string where the desired character is, if it exists. If it doesn't exist, the function returns NULL. Here is the syntax:

char *strchr(const char *str, char c);

Example of strchr

#include <stdio.h>
#include <string.h>
int main(void) {
	char c = 'e';
	char str[] = "pooper";
	int index = strchr(str, c) - str;
	printf("character %c found at index %d of string \"%s\"\n", c, index, str);
	return 0;
}

Searching for a Substring within a String: strstr

strstr in <string.h> library returns a pointer to the first index within a string where a specified substring is found, if it exists. If it doesn't, the function returns NULL. Here is the syntax:

char *strstr(const char *string, const char *substring);

Example of strstr

#include <stdio.h>
#include <string.h>
int main(void) {
	char string[] = "jason siefken";
	char substring[] = "siefken";
	int index = strstr(string, substring) - string;
	printf("substring \"%s\" found in string \"%s\" at index %d\n", substring, string, index);
}
On this page
  • String Library
  • Length of String: strlen
  • Example of strlen
  • Equivalent of strlen without using Copying a String to Another String: strcpy
  • Example of strcpy
  • Equivalent of strcpy without using
  • Copy the first n characters of a string: strncpy
    1. Example of strncpy
  • Concatenating Strings: strcat and strncat
    1. Example of strncat
  • Comparing Strings: strcmp or strncmp
    1. Example of strcmp and strncmp
  • Searching for Character in String: strchr
    1. Example of strchr
  • Searching for a Substring within a String: strstr
    1. Example of strstr