C provides several standard library functions for working with strings.
These functions are mainly declared in the <string.h>
header file. They can be used to find string length, copy strings, join
strings, compare strings, search strings, and perform other string operations.
#include <string.h> when using
the standard string functions discussed in this lesson.
String functions are library functions that make it easier to work with null-terminated C strings.
Examples include:
strlen() – finds string lengthstrcpy() – copies a stringstrcat() – joins stringsstrcmp() – compares strings
Many standard string functions are declared in the
<string.h> header file.
#include <string.h>
Include this header before using these functions.
The strlen() function returns the number of characters in a
null-terminated string, excluding the terminating '\0'.
#include <stdio.h>
#include <string.h>
int main()
{
char name[] = "Rahul";
printf("%zu", strlen(name));
return 0;
}
Output:
5
The null character marks the end of a C string, but it is not included in
the value returned by strlen().
char word[] = "Hello";
printf("%zu", strlen(word));
The result is 5, not 6.
The strcpy() function copies the source string, including its
terminating null character, into the destination array.
char source[] = "Hello";
char destination[20];
strcpy(destination, source);
printf("%s", destination);
The destination must have enough space for the complete string.
The basic syntax is:
strcpy(destination, source);
The contents of source are copied into destination.
The destination should be a writable character array with sufficient capacity.
#include <stdio.h>
#include <string.h>
int main()
{
char student[] = "Amit";
char name[30];
strcpy(name, student);
printf("Student Name: %s", name);
return 0;
}
This copies the contents of student into the writable array
name.
The strcat() function appends the source string to the end of
the destination string.
char first[30] = "Hello ";
char second[] = "World";
strcat(first, second);
printf("%s", first);
Output:
Hello World
The basic syntax is:
strcat(destination, source);
The source is added after the existing characters in the destination string. The destination must have enough free space for the resulting string.
#include <stdio.h>
#include <string.h>
int main()
{
char first[50] = "Rahul ";
char last[] = "Kumar";
strcat(first, last);
printf("Full Name: %s", first);
return 0;
}
The second string is appended to the first string.
The strcmp() function compares two strings character by character.
int result = strcmp("Hello", "Hello");
If the two strings contain the same characters, the result is zero.
The result of strcmp() is interpreted as:
0 → strings are equal0 → first string compares before second string0 → first string compares after second stringThe exact nonzero value is implementation-dependent, so normally test whether the result is less than, equal to, or greater than zero.
#include <stdio.h>
#include <string.h>
int main()
{
char a[] = "Apple";
char b[] = "Apple";
if(strcmp(a, b) == 0)
{
printf("Strings are equal");
}
else
{
printf("Strings are different");
}
return 0;
}
String comparison is case-sensitive.
strcmp("Hello", "hello")
These strings are different because H and h are
different characters.
The strchr() function searches for the first occurrence of a
character in a string.
#include <string.h>
char word[] = "Computer";
char *result = strchr(word, 'p');
If the character is found, the function returns a pointer to its first
occurrence. Otherwise, it returns NULL.
#include <stdio.h>
#include <string.h>
int main()
{
char word[] = "Computer";
if(strchr(word, 'p') != NULL)
{
printf("Character found");
}
else
{
printf("Character not found");
}
return 0;
}
The strstr() function searches for the first occurrence of a
substring inside another string.
char text[] = "I love programming";
char *result = strstr(text, "programming");
If the substring is found, a pointer to its first character is returned.
Otherwise, NULL is returned.
#include <stdio.h>
#include <string.h>
int main()
{
char text[] = "Welcome to C Programming";
if(strstr(text, "Programming") != NULL)
{
printf("Word found");
}
else
{
printf("Word not found");
}
return 0;
}
The strncpy() function copies up to a specified number of
characters from one string to another.
char source[] = "Computer";
char destination[20];
strncpy(destination, source, 8);
destination[8] = '\0';
printf("%s", destination);
Care is required because strncpy() does not always append a
null character when the source length is at least the specified count.
The strncat() function appends up to a specified number of
characters from the source string.
char first[30] = "Hello ";
char second[] = "Programming";
strncat(first, second, 7);
printf("%s", first);
The destination must have enough space for the resulting string and its terminating null character.
The memset() function fills a block of memory with a specified
byte value. It is declared in <string.h>.
char name[10];
memset(name, 0, sizeof(name));
It can be useful for initializing character arrays or other memory regions.
The memcpy() function copies a specified number of bytes from
one memory area to another.
char source[] = "Hello";
char destination[10];
memcpy(destination, source, sizeof(source));
printf("%s", destination);
The destination must have enough space for the copied bytes.
For overlapping memory regions, use memmove() instead.
The memmove() function copies bytes between memory regions even
when the source and destination overlap.
char text[] = "ABCDE";
memmove(text + 1, text, 4);
It is useful when overlapping memory areas must be handled safely.
The memcmp() function compares a specified number of bytes in
two memory areas.
char a[] = "ABC";
char b[] = "ABC";
if(memcmp(a, b, 3) == 0)
{
printf("Same data");
}
Unlike strcmp(), memcmp() compares a specified
number of bytes and does not require null-terminated strings.
The destination array must have enough capacity before using functions such
as strcpy() or strcat().
char source[] = "Hello";
char destination[3];
strcpy(destination, source);
This is unsafe because the destination does not have enough space for the complete string and its null character.
The == operator should not be used to compare the contents of
two C strings.
if(a == b)
{
printf("Same");
}
For string contents, use:
if(strcmp(a, b) == 0)
{
printf("Same");
}
Functions such as strlen(), strcpy(),
strcat(), and strcmp() are declared in
<string.h>.
#include <stdio.h>
#include <string.h>
Including the correct header provides the proper declarations for these standard library functions.
#include <stdio.h>
#include <string.h>
int main()
{
char first[50] = "Hello";
char second[] = " World";
char copy[50];
printf("Length: %zu\n", strlen(first));
strcpy(copy, first);
printf("Copied: %s\n", copy);
strcat(first, second);
printf("Joined: %s\n", first);
if(strcmp(copy, "Hello") == 0)
{
printf("Copy matches Hello");
}
return 0;
}
| Function | Purpose |
|---|---|
strlen() |
Finds string length |
strcpy() |
Copies a string |
strncpy() |
Copies up to a specified number of characters |
strcat() |
Appends a string |
strncat() |
Appends up to a specified number of characters |
strcmp() |
Compares strings |
strchr() |
Searches for a character |
strstr() |
Searches for a substring |
memcpy() |
Copies bytes |
memmove() |
Moves bytes safely when regions overlap |
memset() |
Fills memory with a byte value |
memcmp() |
Compares bytes |
Create a C program that:
strchr().strstr().
Use the appropriate functions from <string.h>.
Make sure all destination arrays have enough capacity.
<string.h>.strlen() finds the number of characters before '\0'.strcpy() copies a string to a destination array.strcat() appends one string to another.strcmp() compares the contents of two strings.strchr() searches for a character.strstr() searches for a substring.strncpy() copies up to a specified number of characters.strncat() appends up to a specified number of characters.memcpy() copies bytes between memory areas.memmove() handles copying between overlapping memory areas.memset() fills a memory area with a byte value.memcmp() compares a specified number of bytes.Question: Which function is used to find the length of a C string?