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.

 

Learn Flutter, FlutterFlow,
Firebase & Angular.

Looking to learn how to create the production-ready apps
from scratch? Well, you are at the right place.

₹1299 or $15.56

Learn to create the Dating App using No-Code Tool FlutterFlow

Level: Advanced

Explore Curriculum
₹1299 or $15.56

Flutter Mobile App Development Course

Explore Curriculum
₹1299 or $15.56

The Complete Angular Course

Explore Curriculum
₹1299 or $15.56

Dart Programming for Absolute Beginners

Coming Soon
FREE

Next.js course

Start Watching
FREE

Fundamentals of Computer Programming Languages

Start Watching
FREE

Learn HTML, CSS & Bootstrap

Start Watching

Latest Web Stories

1

Learn how to open WhatsApp using FlutterFlow | Step by Step Guide

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

Download the Codzify
Mobile App


Learn Anytime, Anywhere at your own pace. Scan the QR Code with your Mobile Camera to Download the Codzify Mobile App.

Codzify Mobile App Codzify Mobile App
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer