Lesson 8 - Strings in the C language
In the previous exercise, Solved tasks for C lesson 7, we've practiced our knowledge from previous lessons.
Lesson highlights
Are you looking for a quick reference on strings and string functions in C instead of a thorough-full lesson? Here it is:
C strings are char
arrays with an extra
terminating character:
{C_CONSOLE}
char text[5] = {'m', 'o', 'o', 'n', '\0'};
printf("%s", text);
{/C_CONSOLE}
Shortened initialization of a char
array string
using quotes:
{C_CONSOLE}
char text[] = "moon";
printf("%s", text);
{/C_CONSOLE}
Getting and setting the character at a given position:
{C_CONSOLE}
char text[] = "moon";
text[0] = 'n';
printf("%c \n", text[0]);
printf("%s \n", text);
{/C_CONSOLE}
Reading strings from the console with the maximum length of
50
:
{C_CONSOLE}
printf("Enter your name: ");
char name[51];
scanf(" %50[^\n]s", name); // [^\n] allows to enter spaces
printf("Hi %s, welcome!", name);
{/C_CONSOLE}
String functions from <string.h>
:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
char text[20] = "moon";
printf("%d \n", strlen(text)); // length
strcat(text, " is in the sky"); // concatenates extra text
printf("%s \n", text);
strcpy(text, "hello!"); // copies given text to the variable
printf("%s \n", text);
printf("%d \n", strcmp("alpha", "bravo")); // compares 2 strings and returns positive or negative number
return (EXIT_SUCCESS);
}
Searching for a character or substring:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char** argv) {
char text[] = "Mr. X strikes again.";
char *p = strchr(text, 'X'); // replace with strstr() to search for a substring
int position = p - text;
if (p != NULL)
{
printf("Found at the position %d", position);
}
else
{
printf("Not found");
}
return (EXIT_SUCCESS);
}
Would you like to learn more? A complete lesson on this topic follows.
We've successfully avoided working with texts in our course. Until now, we have only worked with numbers and single characters. However, we'll need to work with text in most real-world applications. Texts are referred to as strings in programming (a string of characters). The reason we've put this topic off to the side is that C, as a low-level language, doesn't have any data type for storing text and it actually almost doesn't support them. Of course, we can work with texts in the C language, but it's a bit more complicated.
Char array
There are several ways to work with strings in the C language. We're going to
introduce the simplest approach in this lesson - a static string which is an
array of char
s. Consider that we want to store the string
"ICT.social"
, we would need to create the following array of the
char
type in memory:
'I' | 'C' | 'T' | '.' | 's' | 'o' | 'c' | 'i' | 'a' | 'l' | '\0' |
In each "box", there is a single character stored. Notice the extra box at
the end, containing the \0
character which is the null
character. All strings must end with it. Although C
doesn't support strings as a language, it contains standard libraries for
working with them. This is why we have to store strings as it's expected they'll
look like. Meaning that an array representing a string must always be
1
item longer than the length of the text we're
storing!.
Note: Although it's beyond the range of today's lesson, let's mention that the null character is there to determine where the string ends. Aside from static arrays, we can also store strings of any length using pointers, as memory blocks of any length, and it wouldn't be possible without this little aid. We'll teach you everything further along in the courses. An alternative way to specify a string's length is to store it as a number before the first character. This system was used by the Pascal language, however, the null character is a much more common solution.
Let's create a simple example. We'll store some text into a variable and print it to the console:
{C_CONSOLE}
char text[5] = {'m', 'o', 'o', 'n', '\0'};
printf("%s", text);
{/C_CONSOLE}
The result:
Console application
moon
The good news is that the C language allows us to enter text in quotes which
it then converts to a so-called string constant (a char
array
terminated by the \0
character). The code above can be rewritten to
the following form:
{C_CONSOLE}
char text[5] = "moon";
printf("%s", text);
{/C_CONSOLE}
Notice that the array has to be 5
characters long even though
the word "moon" is only 4
letters long. We can even let determining
the length up to the C language:
{C_CONSOLE}
char text[] = "moon";
printf("%s", text);
{/C_CONSOLE}
Unfortunately, we're not able to assign a string constant to an already existing array:
char text[5]; text = "moon"; // This line causes an error printf("%s", text);
This is because it isn't possible for us to assign an array to another array. However, nothing is stopping us from assigning it character by character using a loop or to use functions for copying strings (more on that later on).
Working with single characters
We can work with strings in the same manners as with arrays (because they're actually arrays). Therefore, we are able to change the first character or shorten the string:
{C_CONSOLE}
char text[] = "moon";
text[0] = 'f';
text[3] = '\0';
printf("%s", text);
{/C_CONSOLE}
The result:
Console application
foo
Changing the 4th character to \0
made the string terminate
before that character. Always keep the null character in mind when editing
strings, if you forget to assign it, the program won't know where the string
ends and it'll access memory which doesn't belong to it.
Reading/writing strings
We can read or print strings as we're used to with other data types (we'll
use the %s
modifier). We'll create a string variable as a
char
array and specify a maximal length, e.g. 50 characters (which
is 51
items). We omit the &
characters when
scanning variables using the %s
modifier because we're already
passing an address when passing arrays.
The following program will let you enter your name and greet you:
{C_CONSOLE}
printf("Enter your name: ");
char name[51];
scanf("%50s", name);
printf("Hi %s, welcome!", name);
{/C_CONSOLE}
Notice how the maximal length is specified in the format of a string in the
scanf()
function. If we didn't specify it and encountered an exotic
or just mean user, the characters would overflow from the array and break the
program.
Unfortunately, the scanf()
function terminates the text when
there is an empty space somewhere. If we wanted to read something like
"John Smith"
into a single variable, we'd need to modify the format
string to not stop at anything other than line endings. Modify your line with
the scanning to the following (the space at the beginning is really important
since it won't keep white characters in the buffer):
{C_CONSOLE}
printf("Enter your name: ");
char name[51];
scanf(" %50[^\n]s", name);
printf("Hi %s, welcome!", name);
{/C_CONSOLE}
You may also encounter the functions gets()
or
fgets()
used for reading text from the console. Avoid
gets()
since it doesn't allow us to limit the length of the text
being entered, and fgets()
has to be redirected to the standard
input. Therefore, we'll get along using scanf()
just fine.
Standard string functions
The C language specification provides many functions for working with strings
which will make our programs more simple. To be able to use them, we need to
include the string.h
header file at the beginning of our file:
#include <string.h>
Note: since functions are named using abbreviations, I'll mention the original name for you as well to help you remember them better.
strlen()
- STRing LENgth
We can determine the string's length using strlen()
. It's the
length of the visible part excluding the \0
.
#include <stdio.h>
#include <string.h>
int main(void) {
printf("%d", strlen("moon")); // returns 4
}
strcat
- STRing
conCATenate
We're able to concatenate 2 strings into a single one using the
strcat()
function. Keep in mind that there has to be enough space
for it in the first string.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
char text[20] = "moon";
strcat(text, " is in the sky"); // stores the string "moon is in the sky" to the text variable
printf("%s", text);
return (EXIT_SUCCESS);
}
strcpy()
- STRing CoPY
Since whole arrays cannot simply be copied, there is a function to clone a string into another variable.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
char text[5];
strcpy(text, "moon");
printf("%s", text);
return (EXIT_SUCCESS);
}
strchr()
- STRing CHaR
We can search for a character in a string. It'll be searched from the
beginning to the end and a pointer to it will be returned if the character is
found. Although we can't work with pointers yet, it's enough for us now to know
that if we subtract the string from the pointer, we'll get the position of the
character we're looking for. If the text doesn't contain the character, we'll
get a NULL
value.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char** argv) {
char text[] = "Mr. X strikes again.";
char *p = strchr(text, 'X'); // stores a pointer to the 'X' character in the string
int position = p - text;
if (p != NULL)
{
printf("Found at the position %d", position);
}
else
{
printf("Not found");
}
return (EXIT_SUCCESS);
}
I guess you won't be surprised that the position is zero-based.
strstr()
- STRing subSTRing
We can also search for a string (substring) in a string in the same manner as
we would search for a single character. The function for it is called
strchr()
and is used in the exact same way.
strcmp()
- STRing CoMPare
Compares 2 strings alphabetically and returns a negative number if the first
string is before the second one, 0
if they're equal, and a positive
number if the first one is after the second one.
#include <stdio.h>
#include <string.h>
int main(void) {
printf("%d", strcmp("alpha", "bravo")); // returns a negative number
}
We can also find other versions of the mentioned functions. If we wanted the
C language to work with a string from the end (e.g. search for it starting from
the end), the function for it contains a letter r
in its name (as
in reverse). Specifically, the function is named strrchr()
. We can
also limit the number of characters being processed using the letter
n
(as in number) and specifying said number as an additional
parameter. If the string is longer, it'll only return the part which will be
long as specified. Beware, this part doesn't contain the \0
character. Specifically, this function is called strncat()
.
In the next lesson, Strings in The C language - Working with single characters, we'll continue working with strings in the C language and make several example applications.
Did you have a problem with anything? Download the sample application below and compare it with your project, you will find the error easily.
Download
By downloading the following file, you agree to the license terms
Downloaded 3x (72.46 kB)
Application includes source codes in language C