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

Master String Handling Functions in C in 2023 - Codzify Topics

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 to Build 5 Apps Without Coding using FlutterFlow

  • Lifetime access to all the 5 FlutterFlow Courses
  • Complete FlutterFlow training
  • All future course updates
  • Access via Codzify mobile app
  • 24h 42m of Learning Content
  • 5 FlutterFlow Courses at Codzify
  • Access to Dating App Course, Grocery App Course, Courses App Course, FlutterFlow AI Agents Course, Doctor Appointment Booking Supabase Course.
  • Clone Ready-Made Courses App instantly in FlutterFlow.
  • 30-Day 100% Money-Back Guarantee.

Learn More
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: [email protected]