Article by: Manish Methani
Last Updated: November 2, 2021 at 2:04pm IST
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.
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.
#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; }
stdio.h
and string.h
, to use the string functions in our program.str
and initialize it with the string "Hello, World!".str
and store it in an integer variable len
.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.
#include #include int main() { char src[] = "Hello, World!"; char dest[20]; strcpy(dest, src); printf("The copied string is: %s", dest); return 0; }
stdio.h
and string.h
, to use the string functions in our program.src
and dest
. We initialize src
with the string "Hello, World!" and leave dest
uninitialized.src
to dest
.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.
#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
.