← Back to C Course | Chapter 6: Arrays & Strings | Lesson 7 of 8

C String Functions

String functions are ready-made tools for text, like measuring, copying, joining, and comparing. They save you from looping through each letter yourself.
Syntax
c
#include <string.h>

strlen(string_name);
strcpy(destination, source);
strcat(destination, source);
strcmp(string1, string2);

Finding String Length (strlen)

strlen() has to scan the string character by character until it finds the null terminator, making it an O(n) operation -- calling it repeatedly inside a loop condition (like for (i=0; i<strlen(s); i++)) silently re-scans the whole string on every iteration.

Example: Finding String Length (strlen)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include string functions (strlen, strcpy, ...)
#include <string.h>
// Program execution starts in main()
int main() {
	char str[] = "Hello";
	// Print formatted text to the screen
	printf("%zu", strlen(str));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Copying Strings (strcpy)

strcpy() performs no bounds checking on the destination buffer, so if the source string is longer than the destination array, it overflows into adjacent memory -- strncpy() with an explicit length limit is the safer alternative in most modern code.

Example: Copying Strings (strcpy)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include string functions (strlen, strcpy, ...)
#include <string.h>
// Program execution starts in main()
int main() {
	char dest[20];
	strcpy(dest, "Hello");
	// Print formatted text to the screen
	printf("%s", dest);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Concatenating Strings (strcat)

strcat() has the same overflow risk as strcpy() since it doesn't check whether the destination buffer has enough remaining space for the appended text -- always make sure the destination was allocated large enough to hold both strings combined.

Example: Concatenating Strings (strcat)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include string functions (strlen, strcpy, ...)
#include <string.h>
// Program execution starts in main()
int main() {
	char str[20] = "Hello ";
	strcat(str, "World");
	// Print formatted text to the screen
	printf("%s", str);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Comparing Strings (strcmp)

This return convention mirrors how strcmp mimics alphabetical ordering internally by comparing character codes one pair at a time, so apple compares less than banana because a has a smaller character code than b.

Example: Comparing Strings (strcmp)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include string functions (strlen, strcpy, ...)
#include <string.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", strcmp("apple", "banana"));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Searching Strings (strstr)

strstr() is case-sensitive, so searching for "Cat" inside "concatenate" returns NULL even though "cat" appears in it -- this trips people up often enough that case-insensitive searches usually need a custom loop or platform-specific function like strcasestr().

Example: Searching Strings (strstr)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include string functions (strlen, strcpy, ...)
#include <string.h>
// Program execution starts in main()
int main() {
	char *result = strstr("concatenate", "cat");
	// Print formatted text to the screen
	printf("%s", result);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using strcpy into a buffer that is too small, causing a buffer overflow.
  2. Comparing strings with == instead of strcmp.
  3. Forgetting #include <string.h> before using strlen or strcpy.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.