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

 

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'; ?>