Article by: Manish Methani
Last Updated: September 22, 2021 at 2:04pm IST
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.
The following are some of the most commonly used string handling functions in C:
char string_name[size];
int strlen(char *string);
char* strcpy(char *destination, const char *source);
char* strcat(char *destination, const char *source);
int strcmp(const char *string1, const char *string2);
char* strstr(const char *string1, const char *string2);
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.
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
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!'
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!'
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'
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
A string in C is an array of characters that ends with a null character " ".
You can use the "strlen()" function to find the length of a string in C.
You can use the "strcpy()" function to copy one string to another in C.
You can use the "strcat()" function to concatenate two strings in C.
You can use the "strcmp()" function to compare two strings 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 |