7 Popular String Functions in C With Examples

Strings are an essential data type in programming and are widely used in various applications.

In the C programming language, strings are arrays of characters terminated by a null character (‘\0’).

In this article, we will discuss seven of the most commonly used string functions in C and provide code examples for each.


1. strlen()

The strlen() function calculates the length of a string.

It takes a single argument, which is the string you want to measure, and returns the length of the string (not including the null character).

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

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

Output:

The length of the string is: 13

2. strcpy()

The strcpy() function copies one string to another.

It takes two arguments, the destination string and the source string, and returns a pointer to the destination string.

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

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

Output:

The copied string is: Hello, World!

3. strcat()

The strcat() function concatenates two strings.

It takes two arguments, the destination string and the source string, and returns a pointer to the destination string.

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

int main() {
   char str1[100] = "Hello, ";
   char str2[] = "World!";
   strcat(str1, str2);
   printf("The concatenated string is: %%s\n", str1);
   return 0;
}

Output:

The concatenated string is: Hello, World!

4. strcmp()

The strcmp() function compares two strings.

It takes two arguments, the two strings you want to compare, and returns an integer indicating the relationship between the strings.

If the first string is lexicographically greater than the second string, the function returns a positive integer.

If the first string is lexicographically less than the second string, the function returns a negative integer. If the two strings are equal, the function returns 0.

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

int main() {
   char str1[] = "Hello, World!";
   char str2[] = "Hello, World!";
   int result = strcmp(str1, str2);
   if (result == 0) {
      printf("The two strings are equal.\n");
   } else {
      printf("The two strings are not equal.\n");
   }
   return 0;
}

Output:

The two strings are equal.

5. strchr()

The strchr () function finds the first occurrence of a character in a string.

It takes two arguments, the string you want to search and the character you want to find, and returns a pointer to the first occurrence of the character in the string.

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

int main() {
   char str[] = "Hello, World!";
   char ch = 'o';
   char *ptr = strchr(str, ch);
   printf("The first occurrence of '%%c' in the string is: %%s\n", ch, ptr);
   return 0;
}

Output:

The first occurrence of ‘o’ in the string is: o, World!

6. strstr()

The strstr() function finds the first occurrence of a substring in a string.

It takes two arguments, the string you want to search and the substring you want to find, and returns a pointer to the first occurrence of the substring in the string.

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

int main() {
   char str[] = "Hello, World!";
   char sub[] = "World";
   char *ptr = strstr(str, sub);
   printf("The first occurrence of '%%s' in the string is: %%s\n", sub, ptr);
   return 0;
}

Output:

The first occurrence of ‘World’ in the string is: World!

7. sprintf()

The sprintf() function is used to format and store a string.

It takes a variable number of arguments, the first argument being the destination string and the rest being the data you want to store in the string.

It returns the number of characters written to the destination string.

#include <stdio.h>

int main() {
   char str[100];
   int num = 123;
   sprintf(str, "The number is: %%d", num);
   printf("%%s\n", str);
   return 0;
}

Output:

The number is: 123


These are some of the most commonly used string functions in C programming.

It is important to understand the usage and implementation of these functions in order to effectively manipulate strings in your programs.

In conclusion, strings are an integral part of programming and a good understanding of the various string functions in C can help you to write efficient and effective code.

Whether you are a beginner or an experienced programmer, mastering these functions is an important step in improving your skills.