Skip to main content

Common String Functions in C

String Functions

C provides a number of are pre-written standard library functions that are declared in the string.h header file and helps to work with strings more efficiently thus allows to manipulate strings.

Some of the most commonly used string functions are:

  • strlen: Calculates the length of a string, excluding the null terminator.
  • strcpy: Copies one string to another.
  • strcat: Concatenates two strings.
  • strcmp: Compares two strings lexicographically.
  • strstr: Searches for a substring within a string.
  • sprintf: Writes formatted data to a string.
  • sscanf: Reads formatted data from a string.
  • strchr: Searches for the first occurrence of a character in a string.
  • strrchr: Searches for the last occurrence of a character in a string.
  • strtok: Splits a string into tokens based on a delimiter.
standard library
  • The standard library in C is a collection of functions, macros, and definitions that provide basic functionality for common programming tasks.
  • These tasks include input/output operations, string manipulation, memory allocation, and many others. The standard library functions are defined in a set of header files (e.g. stdio.h, string.h, stdlib.h

Why to use Common String Functions

There are several reasons why you should use common string functions in C:

Standardization

The common string functions are part of the standard C library, which means that they are standardized and have been thoroughly tested. This makes them reliable and portable.

Efficiency

The common string functions have been optimized for performance, so they are generally faster than custom functions that you might write yourself.

Reusability

By using the common string functions, you can avoid reinventing the wheel and save yourself time and effort. These functions are widely used, so you can benefit from the expertise and experience of others who have already worked with them.

Error handling

The common string functions typically handle error conditions in a consistent and reliable way, which helps to avoid bugs and unexpected behavior in your programs.

Examples of Using String Functions

strlen

The strlen function is used to calculate the length of a string.

Example of using strlen in C:

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

int main()
{
char str[] = "Hello, World!";
int length = strlen(str);
printf("The length of the string is: %d\n", length);
return 0;
}
Output:

Explanation

  • The strlen function is called with the string "Hello, World!" as its argument.
  • The function calculates the length of the string by counting the number of characters up to the null terminator ('\0'), which is not included in the length calculation.
  • The result is stored in the variable length, which is then printed to the screen using printf.
note

Note that it's important to include the string.h header file in order to use the strlen function in your program.

strcpy

The strcpy function is used to copy one string to another.

Here's an example of using strcpy in C:

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

int main()
{
char source[] = "Hello, World!";
char destination[20];
strcpy(destination, source);
printf("The copied string is: %s\n", destination);
return 0;
}
Output:

Explanation

  • The strcpy function is called with two arguments: destination and source.
  • The function copies the string in source to the memory location specified by destination.
  • The result of the strcpy function is then printed to the screen using printf.
note

It's important to include the string.h header file in order to use the strcpy function in your program.

strcat

The strcat function is used to concatenate two strings, that is, to join two strings together to form a single string.

Here's an example of using strcat in C:

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

int main()
{
char first[] = "Hello";
char second[] = ", World!";
char result[20];

strcpy(result, first);
strcat(result, second);

printf("The concatenated string is: %s\n", result);

return 0;
}
Output:

Explanation

  • The strcpy function is used to copy the first string to the result string.
  • Then, the strcat function is called with two arguments: result and second.
  • The function concatenates the string in second to the end of the string in result.
  • The result of the strcat function is then printed to the screen using printf.
note

Note that it's important to include the string.h header file in order to use the strcat function in your program.