Strings Functions in C Language - strlen, strcpy,strcmp,strcat functions

Article by: Manish Methani

Last Updated: November 2, 2021 at 2:04pm IST
4 min 40 sec read

Introduction:

String functions are an essential part of any programming language, including C. These functions allow us to manipulate and work with strings of characters efficiently. In this tutorial, we'll discuss some commonly used string functions in C with their code and explanations.

  1. strlen():

The strlen() function in C is used to find the length of a string. The syntax of the function is as follows:

size_t strlen(const char *str);

Here, str is the string whose length we want to find. The function returns the length of the string as a size_t value.

Example:

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

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

Explanation:

  • We start by including the necessary header files, stdio.h and string.h, to use the string functions in our program.
  • We declare a character array str and initialize it with the string "Hello, World!".
  • We use the strlen() function to find the length of the string str and store it in an integer variable len.
  • Finally, we print the length of the string using the printf() function.

Output:

The length of the string is: 13

     2. strcpy():

The strcpy() function in C is used to copy one string to another. The syntax of the function is as follows:

char *strcpy(char *dest, const char *src);

Here, dest is the destination string where we want to copy the source string src. The function returns the destination string after copying.

Example:

#include 
#include 

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

Explanation:

  • We start by including the necessary header files, stdio.h and string.h, to use the string functions in our program.
  • We declare two character arrays src and dest. We initialize src with the string "Hello, World!" and leave dest uninitialized.
  • We use the strcpy() function to copy the contents of src to dest.
  • Finally, we print the copied string using the printf() function.

Output:

The copied string is: Hello, World!

     3. strcat():

The strcat() function in C is used to concatenate two strings. The syntax of the function is as follows:

char *strcat(char *dest, const char *src);

Here, dest is the destination string where we want to concatenate the source string src. The function returns the destination string after concatenating.

Example:

#include 
#include 

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

This program in C demonstrates the use of the strcat() function to concatenate two strings.

The first step is to include the necessary header files stdio.h and string.h. The stdio.h header file provides input/output functions, and string.h provides functions for manipulating strings.

Next, we declare two character arrays str1 and str2 and initialize them with the strings "Hello, " and "World!", respectively.

We then use the strcat() function to concatenate str2 to the end of str1. The strcat() function takes two arguments - the destination string and the source string to be concatenated. In this case, the destination string is str1 and the source string is str2. After the concatenation, the contents of str2 are appended to the end of str1.

Finally, we use the printf() function to print the concatenated string, which is stored in str1, to the console. The %s format specifier is used to print the string.

The output of the program is:

The concatenated string is: Hello, World!

Note that the strcat() function modifies the destination string. In this case, str1 is modified to include the contents of str2.

 

Discover My FlutterFlow Courses and Template Apps

FlutterFlow Course: Basic to Advanced (2025) – Dating App
Learn to Create the Dating App with Admin Dashboard using No-Code Tool FlutterFlow in this comprehensive course in 2025.
FlutterFlow Course: Learn to Build the Grocery Delivery App
Learn to Create Grocery-Delivery App in FlutterFlow, ideal for anyone looking to implement a functional E-Commerce App.
FlutterFlow Course: Learn to Build the Online Courses App
Learn to Create Online Courses App in FlutterFlow, ideal for anyone looking to implement a functional Courses App.
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com