Summary: in this tutorial, you’ll learn about C string and how to manipulate strings effectively.
Introduction to the C Strings
Strings are sequences of characters. C doesn’t have a built-in string type. Instead, it uses an array of characters to define a string. Therefore, a string in C is an array of characters.
To mark the end of a string, C uses a special character '\0'
. The '\0'
is called the null character. When you display the string, the ‘\0’ isn’t shown.
The null character has the ASCII character code 0 (zero). C doesn’t count the null character in the string’s length. But when you define a string, you need to reserve one element in the array to store the null character.
It’s important to notice that the null character is not the same as NULL pointer constant.
The following example defines a string with 6 characters (including the null character), assigns characters to the string elements, and shows the string to the output:
#include <stdio.h>
int main()
{
char message[6];
// assign the string elements
message[0] = 'H';
message[1] = 'e';
message[2] = 'l';
message[3] = 'l';
message[4] = 'o';
message[5] = '\0';
// show the string
printf("%s", message);
return 0;
}
Code language: C++ (cpp)
Output:
Code language: C++ (cpp)Hello
How it works.
- First, define the string
message
which is an array of 6 characters. - Second, assign the characters to individual string elements. To end a string, assign the
'\0'
character to the last element. - Third, display the string to the output. To show a string, you use the
%s
format specifier.
C allows you to initialize a string to a literal string. In this case, the compiler will add the \0
character at the end of the string. For example:
char message[6] = "Hello";
Code language: JavaScript (javascript)
In this example, the string "Hello"
has 5 characters. However, you need to use 6 as the size of the array to store the null character.
To make it simple, you can leave the brackets empty:
char message[] = "Hello";
Code language: JavaScript (javascript)
If you set the size for a string and put more characters into the string, you’ll get the string overflow error.
Assign a literal string to a string variable: strcpy()
To define a string literal, you inculude text in double quotes ""
. For example:
"Hello"
Code language: C++ (cpp)
Since C doesn’t allow you to assign one array to another, you cannot assign a literal string to a string variable like this:
message = "Hello";
Code language: C++ (cpp)
Instead, you need to use the standard library function strcpy()
to copy a string literal to a variable. The strcpy()
copies the whole string including the null character. To use the strcpy()
function, you need to include the string.h
standard library.
The following example shows how to initialize the message
string to "Hello"
:
#include <stdio.h>
#include <string.h>
int main()
{
char message[6];
// assign the string "Hello" to message
strcpy(message, "Hello");
printf("%s", message);
return 0;
}
Code language: C++ (cpp)
Get the length of a string: strlen()
C uses variable-length strings. For example:
#include <stdio.h>
#include <string.h>
int main()
{
char first_name[100] = "John";
printf("%s", message);
return 0;
}
Code language: C++ (cpp)
This example defines a string that can hold up to 100 characters. The size of the array is 100. But the length of the string is 4.
To get the length of a string, you use the standard library function strlen()
:
#include <stdio.h>
#include <string.h>
int main()
{
// assign the "John" to the string
char first_name[100] = "John";
// get the length of the string
int len = strlen(first_name);
printf("The length of the first_name is %d", len);
return 0;
}
Code language: C++ (cpp)
Concatenate two strings into one: strcat()
To concatenate two strings into one, you use the standard library function strcat()
. For example:
#include <stdio.h>
#include <string.h>
int main()
{
char first_name[25] = "John",
last_name[25] = "Doe",
full_name[50];
// copy the first name to the full name
strcpy(full_name, first_name);
// copy the " " to the full name
strcat(full_name, " ");
// copy the last name to the full name
strcat(full_name, last_name);
// show the full name
printf("%s", full_name);
return 0;
}
Code language: C++ (cpp)
Output:
Code language: C++ (cpp)John Doe
How it works.
First, declare three variables to hold three strings first_name
, last_name
, and full_name
:
char first_name[25] = "John",
last_name[25] = "Doe",
full_name[50];
Code language: C++ (cpp)
Second, copy the string first_name
to full_name
:
// copy the first name to the full name
strcpy(full_name, first_name);
Code language: C++ (cpp)
Third, concatenate the string " "
and full_name
:
// concatenate the " " to the full name
strcat(full_name, " ");
Code language: C++ (cpp)
Fourth, concatenate the last_name
to the full_name
:
// concatenate the last name to the full name
strcat(full_name, last_name);
Code language: C++ (cpp)
Finally, show the full_name
string to the output:
// show the full name
printf("%s", full_name);
Code language: C++ (cpp)
Read a string from the standard input
The standard function fgets()
allows you to read a string from the keyboard (or standard input). Here’s the general syntax of the fgets()
function:
fgets(name, sizeof(name), stdin)
Code language: C++ (cpp)
In this syntax:
- The
name
specifies a string variable. - The
sizeof(name)
indicates the maximum number of characters to read (plus one for the null character). - The
stdin
is the standard input, which is the keyboard in this case.
If you press Enter, the fgets()
function also reads the end-of-line character (\n
) from the keyboard.
The following example shows how to read a string from the keyboard:
#include <stdio.h>
#include <string.h>
int main()
{
char message[100];
// get the string from the input
fgets(message, sizeof(message), stdin);
// show the input string
printf("%s", message);
printf("The length of the string is %d\n", strlen(message));
// display the characters
for (int i = 0; i < strlen(message); i++)
{
if (message[i] == '\0')
printf("message[%d] = NUL\n", i);
else if (message[i] == '\n')
printf("message[%d] = NEW_LINE\n", i);
else
printf("message[%d] = %c\n", i, message[i]);
}
return 0;
}
Code language: C++ (cpp)
If you enter the mesage hi
and press Enter, you’ll see the following output:
hi
hi
The length of the string is 3
message[0] = h
message[1] = i
message[2] = NEW_LINE
Code language: C++ (cpp)
As you can see, the last character of the string is the new line character (\n
), not NUL character (\0
).
To fix this, you need to terminate the string with the \0:
message[strlen(message) - 1] = '\0';
Code language: C++ (cpp)
The following program fixes the string read from the keyboard by terminating it with the NUL character ('\0'
):
#include <stdio.h>
#include <string.h>
int main()
{
char message[100];
// get the string from the input
fgets(message, sizeof(message), stdin);
message[strlen(message) - 1] = '\0';
// show the input string
printf("%s\n", message);
printf("The length of the string is %d\n", strlen(message));
return 0;
}
Code language: C++ (cpp)
Output:
hi
hi
The length of the string is 2
Code language: C++ (cpp)
Compare two strings
To compare two strings, you use the strcmp()
function. The strcmp()
compares the first character of each string, if they are equal, it continues the second character in each string until the characters differ or until it reaches the NUL character.
The strcmp()
returns -1
if the first string before the second string, 0 if two strings are equal, and 1 if the first string is after the second string.
See the following example:
#include <stdio.h>
#include <string.h>
int main()
{
char sender[25] = "Alice",
receiver[25] = "Bob";
int result = strcmp(sender, receiver);
printf("%d", result); // -1
return 0;
}
Code language: C++ (cpp)
Output:
-1
Code language: C++ (cpp)
The program shows -1 indicating that the string "Alice"
is before "Bob"
.
Summary
- In C, a string is an array of characters. A string uses the null character (
'\0'
) to indicate the end of a string. - Use the
strcpy()
function to copy a string to another. - Use the
strlen()
function to get the length of a string. - Use the
strcat()
function to concatenate two strings into one. - Use the
strcmp()
function to compare two strings. - Use the
%s
format specifier in theprintf()
function to show a string to the output.