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.

 

Simplified Coding for Beginners.








Download the Codzify
Mobile App


Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.

Premium

The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum
Free

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching
Premium

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum
Free

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test
include_once 'codzify-footer.php'; ?>