String Functions in C

Topics Covered
Get FREE counselling from experts
Learn more about SCALER Premium Programs
Know more

Overview

Strings are an array of characters that terminate with a null character '\0'. The difference between a character array and a string is that, unlike the character array, the string ends with a null character. There are various built-in string functions in the C programming language.

Introduction to C String Functions

We're often required to modify the strings and perform several operations on them according to our needs. If we want to get the length of the string, we could run a loop and calculate its length, but it is not the best way in case of complex problems. Hence, string functions are used to make our code efficient and straightforward as they are pre-written so we can use them directly.

The string handling functions are defined in the header file string.h. This header file must be included in the C program to use the string handling functions.

String Declaration

There are two ways to declare strings in C:

  1. The following example will create a string as "Scaler" where the last character must always be a null character. The size mentioned within the brackets is the maximum number of characters a string could hold, and it is mandatory to give the size of a string if we are not initializing it at the time of declaration.
  1. In this method, we do not need to put the null character at the end of the string constant. The compiler automatically inserts the null character at the end of the string.

String Functions

The following are the string functions in C:

FunctionDescription
strlen()It returns the string's length.
strnlen()It returns the specified value if the value specified is less than the string length, otherwise the string length.
strcmp()It compares two strings and returns 0 if the strings are the same.
strncmp()It compares two strings only to n characters.
strcat()It concatenates two strings and returns the concatenated string.
strncat()It concatenates n characters of one string to another string.
strcpy()It copies one string into another.
strncpy()It copies the first n characters of one string into another.
strchr()It finds out the first occurrence of a given character in a string.
strrchr()It finds out the last occurrence of a given character in a string.
strstr()It finds out the first occurrence of a given string in a string.
strnstr()It finds out the first occurrence of a given string in a string where the search is limited to n characters.
strcasecmp()It compares two strings without sensitivity to the case.
strncasecmp()It compares n characters of one string to another without sensitivity to the case.

Examples of String Functions in C

Let's look at an example to understand how string functions work in C.

strlen()

Syntax

size_t represents long unsigned int.

It takes a string (character array or character pointer) as input and writes the length of that string without including the end character '\0'.

Program Code

Output

strnlen()

Syntax

size_t represents long unsigned int.

strnlen() take a string and a positive integer maxlen as input and return the length of the string if maxlen is greater than the size of the string otherwise, always return maxlen which means it only counts the characters till str[maxlen -1].

Program Code

Output

strcmp()

Syntax

strcmp() takes two strings as input, then compares them, and returns an integer based on the following condition:

ExpressionReturns
string 1 > string 2Positive integer
string 1 < string 2Negative
string 1 = string 2Zero

Program Code

Output

strncmp()

Syntax

size_t is for long unsigned int.

It compares only the first n characters of both the strings and returns an integer value accordingly:

ExpressionReturns
string 1(first n characters) > string 2(first n characters)Positive integer
string 1(first n characters) < string 2(first n characters)Negative
string 1(first n characters) = string 2(first n characters)Zero
Program Code

Output

strcat()

Syntax

It takes two strings as input and concatenates the second string to the first string, which means it will attach the second string to the end of the first string and save the concatenated string to the first string. The size of the first string should be large enough to save the result.

Program Code

Output

strncat()

Syntax

It takes two strings as input and attaches only the first n characters of the second string to the end of the first string.

Program Code

Output

strcpy()

Syntax

It takes two strings as input and overwrites the data of the second string into the first string, i.e., it copies the data of the second string to the first string.

Program Code

Output

strncpy()

Syntax

size_t is long unsigned int and n is an integer.

It takes two strings as input and overwrites the data of the first string by the second string based on specific conditions:

If the length of string2 is greater than n, it copies only the first n characters of string2 to string1; otherwise, it copies the entire string2 to string1.

Program Code

Output

strchr()

Syntax

It takes a string and a character as input and finds out the first occurrence of the given character in that string. It will return the pointer to the first occurrence of that character; if found otherwise, return Null.

Program Code 1

  • When the character is present in the given string

Output

Program code 2

  • When the character is not present in the given string Notice the character 'z' is not present in the provided string. In such a case, it returns a null value.

Output

strrchr()

Syntax

It takes a string and a character as input and finds out the last occurrence of a given character in that string. It will return the pointer to the last occurrence of that character if found otherwise, return Null.

Program Code 1

Output

Program Code 2

  • When the character is not present in the given string Notice the character 'z' is not present in the provided string. In such a case, it returns a null value.

Output

strstr()

Syntax

It takes two strings as input and finds out the first occurrence of the second string in the first string. It will return a pointer that points to the start of the first occurrence of the second string in the first string and a Null if the second string is not present in the first string.

Program Code

Output

strcasecmp()

Syntax

It takes two strings as input and compares them irrespective of their case sensitivity.

IfReturns
str1 < str2Less than 0
str1 = str2 (ignoring case)0
str1 > str2Greater than 0

Program Code

Output

strncasecmp()

Syntax

It takes two strings as input and compares them till n characters irrespective of their case sensitivity. Program Code

Output

Conclusion

  • By the end of the article, you will be familiar with string data type and its declaration in C.
  • Character arrays and strings are different in that a string ends with a null character, while character arrays do not.
  • The article covers C's various built-in string functions and their examples.
  • The string functions in C are easy to use and increase efficiency.