The C string functions are built-in functions that can be used for various
operations and manipulations on strings. These string functions can be
used to perform tasks such as string copy, concatenation, comparison,
length, etc. The <string.h> header file contains these string functions.
C String Functions
The C string functions are built-in functions that can be used for
various operations and manipulations on strings. These string
functions can be used to perform tasks such as string copy,
concatenation, comparison, length, etc. The <string.h> header
file contains these string functions.
String Functions in C
Some of the commonly used string functions in C are as follows:
1. strcat() Function
The strcat() function in C is used for string concatenation. It will
append a copy of the source string to the end of the destination
string.
Syntax
char* strcat(char* dest, const char* src);
The terminating character at the end of dest is replaced by the
first character of src.
Parameters
dest: Destination string
src: Source string
Return value
The strcat() function returns a pointer to the dest string.
Example
1
// C Program to illustrate the strcat function
2
#include <stdio.h>
3
4
int main()
5
{
6
char dest[50] = "This is an";
7
char src[50] = " example";
8
9
printf("dest Before: %s\n", dest);
10
11
// concatenating src at the end of dest
12
strcat(dest, src);
13
14
printf("dest After: %s", dest);
15
16
return 0;
17
}
Output
dest Before: This is an
dest After: This is an example
2. strlen() Function
The strlen() function calculates the length of a given string. It
doesn’t count the null character ‘\0’.
Syntax
int strlen(const char *str);
Parameters
str: It represents the string variable whose length we
have to find.
Return Value
strlen() function in C returns the length of the string.
Example
1
// C program to demonstrate the strlen() function
2
#include <stdio.h>
3
#include <string.h>
4
5
int main()
6
{
7
// Declare and initialize a character array 'str' with
8
// the string "GeeksforGeeks"
9
char str[] = "GeeksforGeeks";
10
11
// Calculate the length of the string using the strlen()
12
// function and store it in the variable 'length'
13
int length = strlen(str);
14
15
// Print the length of the string
16
printf("String: %s\n", str);
17
18
printf("Length: %d\n", length);
19
20
return 0;
21
}
Output
String: GeeksforGeeks
Length: 13
3. strcmp() Function
The strcmp() is a built-in library function in C. This function takes
two strings as arguments and compares these two strings
lexicographically.
Syntax
int strcmp(const char *str1, const char *str2);
Parameters
str1: This is the first string to be compared.
str2: This is the second string to be compared.
Return Value
If str1 is less than str2, the return value is less than 0.
If str1 is greater than str2, the return value is greater than
0.
If str1 is equal to str2, the return value is 0.
Example
1
// C program to demonstrate the strcmp() function
2
#include <stdio.h>
3
#include <string.h>
4
5
int main()
6
{
7
// Define a string 'str1' and initialize it with "Geeks"
8
char str1[] = "Geeks";
9
// Define a string 'str2' and initialize it with "For"
10
char str2[] = "For";
11
// Define a string 'str3' and initialize it with "Geeks"
12
char str3[] = "Geeks";
13
14
// Compare 'str1' and 'str2' using strcmp() function and
15
// store the result in 'result1'
16
int result1 = strcmp(str1, str2);
17
// Compare 'str2' and 'str3' using strcmp() function and
18
// store the result in 'result2'
19
int result2 = strcmp(str2, str3);
20
// Compare 'str1' and 'str1' using strcmp() function and
21
// store the result in 'result3'
22
int result3 = strcmp(str1, str1);
23
24
// Print the result of the comparison between 'str1' and
25
// 'str2'
26
printf("Comparison of str1 and str2: %d\n", result1);
27
// Print the result of the comparison between 'str2' and
28
// 'str3'
29
printf("Comparison of str2 and str3: %d\n", result2);
30
// Print the result of the comparison between 'str1' and
31
// 'str1'
32
printf("Comparison of str1 and str1: %d\n", result3);
33
34
return 0;
35
}
Output
Comparison of str1 and str2: 1
Comparison of str2 and str3: -1
Comparison of str1 and str1: 0
4. strcpy
The strcpy() is a standard library function in C and is used to copy
one string to another. In C, it is present in <string.h> header file.
Syntax
char* strcpy(char* dest, const char* src);
Parameters
dest: Pointer to the destination array where the content
is to be copied.
src: string which will be copied.
Return Value
strcpy() function returns a pointer pointing to the output
string.
Example
1
// C program to illustrate the use of strcpy()
2
#include <stdio.h>
3
#include <string.h>
4
5
int main()
6
{
7
// defining strings
8
9
char source[] = "GeeksforGeeks";
10
char dest[20];
11
12
// Copying the source string to dest
13
strcpy(dest, source);
14
15
// printing result
16
printf("Source: %s\n", source);
17
printf("Destination: %s\n", dest);
18
19
return 0;
20
}
Output
Source: GeeksforGeeks
Destination: GeeksforGeeks
Array of Strings in C
In C, an array of strings is a 2D array where each row contains a
sequence of characters terminated by a ‘\0’ NULL character
(strings). It is used to store multiple strings in a single array.
Let’s take a look at an example:
1
#include <stdio.h>
2
int main() {
4
// Creating array of strings for 3 strings
6
// with max length of each string as 10
7
char arr[3][10] = {"Geek", "Geeks",
8
"Geekfor"};
9
10
for (int i = 0; i < 3; i++)
11
printf("%s\n", arr[i]);
12
return 0;
13
Output
Geek
Geeks
Geekfor
An array of strings allows you to store and manipulate text in C.
Syntax of Array of Strings
char arr_name[r][m] = {s1, s2, …., sn};
Here,
arr_name: Name of the variable.
r: Maximum number of strings to be stored in the array
m: Maximum number of character values that can be
stored in each string.
s1, s2, …., sn: Strings to be stored.
Invalid Operations in Arrays of Strings
We can’t directly change or assign the values to an array of
strings in C.
Example:
char arr[3][10] = {“Geek”, “Geeks”, “Geekfor”};
arr[0] = “GFG”;
// This will give an Error that says assignment to expression with
an array type.
To change values we can use strcpy() function in C:
strcpy(arr[0],”GFG”);
// This will copy the value to the arr[0].
How Array of Strings are Represented in
Memory?
Consider the array of string:
char arr[3][10] = {“Geek”, “Geeks”, “Geekfor”};
It will be stored in the format shown below:
We have 3 rows and 10 columns specified in our array of string
which means that each string can be as long as 1o characters.
But as we can see, there are many strings that takes less space
than 10 but still allocated the same space due to the property of
2D array. It leads to so much wastage of space. But we can avoid
this wastage in our program by using array of pointers to strings.
Array of Strings Using Pointers
In C we can use an Array of pointers instead of having a 2-
Dimensional character array. It is a single-dimensional array of
Pointers where pointer to the first character of the string literal is
stored.
Let’s take a look at an example
1
#include <stdio.h>
2
3
int main() {
4
5
// Array of pointers to strings
6
char *arr[] = {"Geek", "Geeks", "Geekfor"};
7
8
for (int i = 0; i < 3; i++)
9
printf("%s\n", arr[i]);
10
return 0;
11
}
Output
Geek
Geeks
Geekfor
Syntax
char *arr[] = { “Geek”, “Geeks”, “Geekfor” };
char *: It indicates that each element in the array is a
pointer to a character (i.e., each points to a string).
arr[]: Declares an array of pointers, where the size is
determined by the number of strings in the initialization
list.
= { “Geek”, “Geeks”, “Geekfor” }: Initializes the
array, with each pointer pointing to a string: arr[0] points
to “Geek”, arr[1] to “Geeks”, and arr[2] to “Geekfor”.
How Array of String Pointers Represented in
Memory?
Unlike the 2D array of characters, array of pointer to strings
store the pointer to the array. So, each string is only take the
required amount of memory. Though some extra space is
required to store the pointers.