Strings are often used in the C language. Through them, a program displays the messages or results to the user, enters the information into the system, generates or consults the information in databases, compares or searches for words in a text, etc. They are also used for the functions to specify the file paths, select the working directories, etc.
In this Linux Hint article, you will learn all about strings. We will explain from scratch what a string is, what elements it consists, and how it is created.
We will also see a special section which explains what special characters are and what role does the control characters play in a string.
After the theoretical explanation, we will see the several practical examples with code snippets and images where we teach you how to create the strings and use the basic functions that the C language provides for their management.
Strings in C Language
A string is a piece of text which consists of alphanumeric characters, symbols, and control characters. The compiler processes anything that is enclosed in quotes as a string and automatically places a null character after the last character to indicate the end.
Next, let us look at the most well-known chain in programming:
The C language stores the strings in arrays of elements of type char or unsigned char. The data type of the elements must be specified in the declaration of the array as shown in the following:
unsigned char string_2[20];
In this case, the string_1 array has 20 elements of type char and the string_2 array has 20 elements of type unsigned char.
The data type of the array elements determines the character set that the string accepts. A char data type accepts only one 7-bit ASCII character. The remaining bit is used as a sign and parity check, so only the characters between 0h00 and 0h80 from the ASCII table can be represented in this data type.
The characters of the unsigned char type do not use a sign bit, so their 8 bits are used to represent the characters between 0h00 and 0hFF of the extended ASCII code.
In addition to alphanumeric characters and symbols, a string can also contain the control characters.
Escape and Control Characters in the C Language
Control characters have the function of performing specific actions in the string that contains them. For example, an “\n” character before the beginning of a string means that it should be written to a new line.
The escape character at the end of the string indicates that the string is to be written and the cursor is moved to the next line.
All escape sequences consist of an alphanumeric character which is preceded by a backslash.
Next, we see a table of the most commonly used escape characters in the C language:
\n | New line |
\r | Carriage return |
\a | Alert beep |
\t | Tabulation |
\v | Vertical tabulation |
\b | Backspace |
\f | Form feed |
In addition to these escape characters, there are sequences that indicate to the compiler that it must include certain symbols in the string. For example, if we want to include a double quote character in the text, the compiler would interpret it as a terminator of the string and not as another character of it. For this reason, some symbols must be preceded by a backslash so that the compiler can insert. In the following example, we see what these are:
\” | Insert double quotes |
\’ | Insert single quotes |
\\ | Insert backslash |
\? | Insert question mark |
:
How to Create a String in the C Language
One of the ways to declare a string is to create a character array of type char or unsigned char of a certain size, as follows:
str = "Hello World";
In this way, the compiler treats the data that is stored in the array as a single string by assigning a null character at the end of it.
How to Create a Constant String in the C Language
To create a constant string, you must declare a character array of type char or unsigned char without specifying its size. Then, assign the string to it as shown in the following:
The compiler automatically puts a null character at the end of the string to indicate its end. With this type of string, it is not possible to change the contents.
How to Create a Pointer to the String in the C Language
Another way to store a string is to declare a pointer to the array that stores it. This method dynamically allocates the size of the character array. Here is the correct way to declare a string pointer:
s_Ptr ="Hello world";
The pointer method is the most used by the C language functions to refer to a string in their input and output arguments.
Functions for Handling the Strings
When programming, it is important to memorize the basic functions for handling the strings because we use them over and over again. If you master them, your work will go much faster.
All the functions for handling the strings are defined in the “string.h” header. To make use of them, you must include this file in your code as follows:
In the following section, we will briefly show you how to use the basic string handling functions in the C language.
How to Display a String in the C Language
The most common way to display a string on the screen is to use the printf() or puts() functions, both of which are defined in the “stdio.h” header. These functions accept a string or a pointer to a string as an input argument.
Now, let us see how a string can be displayed on the screen using the printf() function:
If you use the printf() function and pass a string pointer as in the second case, you must specify in what format it should be displayed. Since it is a string in this case, this format is specified with “%s” as the first input argument.
In the following figure, you can see the compilation and execution of this code:
How to Copy a String in C Language
There are several ways to copy a string in the C language. The most commonly used method is the strcpy() function which is defined in the “string.h” header. In the following example, we will look at the syntax of this function:
The strcpy() function copies the “s2” string to “s1” string and inserts a null character at the end. The “s2” string can be specified explicitly or implicitly.
Example:
The following code copies the explicitly specified “s2” string to “s1” string and displays it in the command console:
The following image shows the compilation and execution of this code. In this case, strcpy() copies the string from “s2” to “s1” and then prints it to the screen using the printf() function:
How to Concatenate Two Strings in the C Language
The function that the C language provides to concatenate two strings is strcat(). Let us look at the syntax of this function in the following:
This function concatenates the “s2” string into “s1” string. Like all string processing functions, strcat() places a null character at the end of the concatenated string. Both “s1” and “s2” can be specified explicitly or implicitly.
Example:
The following image shows the compilation and execution of this code. In this case, strcat() concatenates the “s2” sring to “s1” string and then prints it to the screen using the printf() function:
How to Compare Two Strings in the C Language
The function that the C language provides to compare the strings is strcmp(). Let us look at the syntax of this function in the following:
The strcmp() function compares the “s1” and “s2” strings. If they are equal, it returns a result that is equal to 0. If they are not equal, it returns a result that is not equal to 0.
#include <string.h>
void main ()
{
char s1 [50]="equal";
char s2 [50]="equal";
char s3 [50]="not equal";
//case 1
if (strcmp (s1, s2) ==0)
printf ("\n\ns1 and s2 are equal\n\n" );
else
printf ("\n\ns1 and s2 are not equal\n\n" );
//case 2
if (strcmp (s1, s3) ==0)
printf ("\n\ns1 and s3 are equal\n\n" );
else
printf ("\n\ns1 and s3 are not equal\n\n" );
}
We see the compilation and execution of this code in the following figure. In case 1, “s1” and “s2” are the same, so strcmp() returns 0. In case 2, “s1” and “s3” are not equal, so the program enters the “else” of the “if” statement.
How to Cut a String Into Fragments
The strsep() function fragments the string that is pointed to by stringp into pieces, starting with a delimiter that is specified in its “delim” input argument. Let us now look at the syntax of this function:
When strsep() encounters a delimiter, it replaces it with a null character which marks the end of the new fragment and sets the pointer to stringp to the next position. This function returns the pointer to the new fragment on each call, or a null pointer if it finds no more delimiters.
Example:
In this example, we fragment the “Today is a great day for programming” string and use a space as a delimiter so that the string is fragmented word by word, which we output to the command console using the printf() function. The call to the strsep() function is repeated in a while loop until it finds no more delimiter and returns a null pointer.
#include <string.h>
void main ()
{
char s_in [50]= "Today is a great day for programming";
char* in_Ptr = s_in;
char del [4] = " ";
char* o_Ptr;
while (o_Ptr != NULL){
o_Ptr = strsep( &in_Ptr, del);
printf("%s\n\n", o_Ptr);}
}
The following image shows the compilation and execution of this code. As we can see, strsep() fragments the string word by word and the printf() function prints each word to a new line of the command console:
Conclusion
In this Linux Hint article, we explained what a string consists of in the C language.
We learned what elements they are made of, what kind of data they use, how they are encoded, and what characters each type supports.
We also explained the different ways to declare the arrays or pointers to store the strings, as well as the instructions to allocate them.
To help you get a grip with strings in this language, we used the practical examples and code snippets to show you what strings are and how to use some of the basic string processing functions.