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

Master String Handling Functions in C in 2023

Article by: Manish Methani

Last Updated: September 22, 2021 at 2:04pm IST
12 min 14 sec

Introduction to String Handling functions

In C programming language, a string is a sequence of characters terminated by the null character '�'. String handling functions in C are a set of built-in functions that are used to perform operations on strings, such as finding the length of a string, copying a string, concatenating two strings, comparing two strings, and searching for a substring in a string.

String handling functions in C

The following are some of the most commonly used string handling functions in C:

Declaration of strings in C

char string_name[size];

String length function in C

int strlen(char *string);

String copy function in C

char* strcpy(char *destination, const char *source);

String concatenation function in C

char* strcat(char *destination, const char *source);

String compare function in C

int strcmp(const char *string1, const char *string2);

String search function in C

char* strstr(const char *string1, const char *string2);

Declaration of strings in C

To declare a string in C, you can use the following syntax:

char string_name[size];

Here, string_name is the name of the string, and size is the maximum number of characters that the string can hold.

String length function in C

The strlen() function is used to find the length of a string. It takes a string as input and returns the length of the string.

int strlen(char *string);

Here's an example of how to use the strlen() function:

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

int main()
{
    char name[50] = "John Doe";
    int length = strlen(name);
    
    printf("The length of the string '%s' is %d
", name, length);
    
    return 0;
}

Output:

The length of the string 'John Doe' is 8

String copy function in C

The strcpy() function is used to copy one string to another. It takes two strings as input - the destination string and the source string - and copies the contents of the source string to the destination string.

char* strcpy(char *destination, const char *source);

Here's an example of how to use the strcpy() function:

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

int main()
{
    char source[] = "Hello, World!";
    char destination[20];
    
    strcpy(destination, source);
    
    printf("The source string is '%s'", source);
    printf("The destination string is '%s'", destination);
    
    return 0;
}

Output:

The source string is 'Hello, World!'
The destination string is 'Hello, World!'

String concatenation function in C

The strcat() function is used to concatenate two strings. It takes two strings as input - the destination string and the source string - and concatenates the contents of the source string to the end of the destination string.

char* strcat(char *destination, const char *source);

Here's an example of how to use the strcat() function:

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

int main()
{
    char destination[50] = "Hello, ";
    char source[] = "World!";
    
    strcat(destination, source);
    
    printf("The concatenated string is '%s'", destination);
    
    return 0;
}

Output:

The concatenated string is 'Hello, World!'

String compare function in C

The strcmp() function is used to compare two strings. It takes two strings as input - string1 and string2 - and returns an integer value that indicates whether the two strings are equal, or whether one string is greater than or less than the other.

int strcmp(const char *string1, const char *string2);

Here's an example of how to use the strcmp() function:

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

int main()
{
    char string1[] = "apple";
    char string2[] = "banana";
    int result = strcmp(string1, string2);
    
    if (result == 0)
    {
        printf("The two strings are equal");
    }
    else if (result < 0)
    {
        printf("'%s' is less than '%s'", string1, string2);
    }
    else
    {
        printf("'%s' is greater than '%s'", string1, string2);
    }
    
    return 0;
}

Output:

'apple' is less than 'banana'

String search function in C

The strstr() function is used to search for a substring in a string. It takes two strings as input - the main string and the substring to be searched for - and returns a pointer to the first occurrence of the substring in the main string.

char* strstr(const char *string1, const char *string2);

Here's an example of how to use the strstr() function:

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

int main()
{
    char string1[] = "The quick brown fox jumps over the lazy dog";
    char string2[] = "brown";
    char *result = strstr(string1, string2);
    
    if (result == NULL)
    {
        printf("Substring not found");
    }
    else
    {
        printf("Substring '%s' found at position %ld", string2, result - string1 + 1);
    }
    
    return 0;
}

Output:

Substring 'brown' found at position 10

FAQ

1. What is a string in C?

A string in C is an array of characters that ends with a null character "".

2. How do I find the length of a string in C?

You can use the "strlen()" function to find the length of a string in C.

3. How do I copy one string to another in C?

You can use the "strcpy()" function to copy one string to another in C.

4. How do I concatenate two strings in C?

You can use the "strcat()" function to concatenate two strings in C.

5. How do I compare two strings in C?

You can use the "strcmp()" function to compare two strings in C.

6. How do I search for a substring in a string in C?

You can use the "strstr()" function to search for a substring in a string in C.

That's it for this tutorial on string-handling functions in C programming. We hope you found it helpful! If you have any questions or feedback, please feel free to leave a comment below.

Here's a summary of the string-handling functions covered in this tutorial:

Function Description
strlen() Returns the length of a string
strcpy() Copies one string to another
strcat() Concatenates two strings
strcmp() Compares two strings
strstr() Searches for a substring in a string

 

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