1. C Programming for Beginners: Writing Your First Hello World Program in C in 2023 2. Mastering Datatypes in C Programming: A Comprehensive Guide for Beginners in 2023 3. Complete Guide to % Format Specifiers in C for Beginners in 2023 4. A Comprehensive Guide to Operators in C Programming: Learn Everything About Operators in C in 2023 5. Master C Operator Precedence and Associativity in 2023: Definition, Examples, and Code 6. Learn Increment and Decrement Operators in C: A Beginners Guide with Examples - Codzify 7. Mastering Bitwise Operators in C: A Comprehensive Guide with Examples - Codzify 8. Ternary Operators in C Programming: A Beginners Guide (2023) 9. Switch Statements in C 2023: Complete Guide for Beginners 10. Learn Functions in C Programming - A Complete Tutorial for Beginners in 2023 11. Call by Value and Call by Reference in C Programming - A Complete Tutorial for Beginners in 2023 12. Storage Classes in C Programming: A Comprehensive Guide for 2023 13. Arrays in C Programming: A Comprehensive Guide for 2023 14. Basics of 2D Array in C Programming in 2023: Definition, Initialization, and Accessing Elements 15. Beginners Guide to Strings in C Programming with Codes and Examples 16. Strings Functions in C Language - strlen, strcpy,strcmp,strcat functions 17. A Comprehensive Guide to Structures in C Programming in 2023 18. Learn Unions in C Programming with Codes, Examples, and Explanations in 2023 19. Dynamic Memory Allocation in C: Understanding malloc, calloc, realloc, and free Functions 20. Introduction to C Programming 21. Hello world program in C 22. String functions in C - strncat, strcpy, strncpy, strchr, strrchr, strstr functions 23. Program for Fibonacci number in C - Codzify.com 24. What is palindrome number in C? 25. Write a C program to find a factorial of a given number 26. Write a C program to check whether a given number is prime or not 27. Write a C program to check whether a given number is an Armstrong number or not. 28. Write a C Program to transpose a matrix 29. C Programming Tutorial: Understanding Comments and Identifiers with Code Examples 30. Master String Handling Functions in C in 2023 - Codzify Topics

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.

 

Explore Tech Guide:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com