Strings in C Programming

Strings in C Programming

A Comprehensive Guide to Working with Strings in C

Introduction to Strings in C

What is a String?

Strings in C are what we call in the English language a word, a group of words, a character of a group of characters.

In a more technical sense, strings in C are arrays of characters with a null character ('\0') at the end which marks the end of the string.

So anywhere you see a string or strings in C, you should put it at the back of your mind that it is an array, but this time around an array of characters, because the string is made up of characters, and all characters belong to the char data type, and since they are of the same data type, we can simply use an array to store them as one.

To understand what an array is, you can read it here.

How are Strings Stored in C

There are various ways in which a string can be stored in C, and that depends on whether it is stored as:

  1. a String constant

  2. a Character constant

  3. String Constant: a string constant is a series of characters enclosed in a double quotation (" "). In this case, all the characters are joined together to form a string.

    For example, let us consider the string: Hello World, to express it as a string constant, we are going to have:

    "Hello World"

    Here you can see that the entire words Hello and World including the space that is between them are all placed inside the double quotation.

  4. Character Constant: a character constant is a character that is enclosed in a single quotation (' '). In this case, characters are placed individually in a single quotation as a standalone.

    Using the same example as the one above, we are going to have:

    'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'

    Here, we can see that each of the characters was all put inside a single quotation mark, including the space between the Hello and World, as space is also a character in C.

The Null Character

In C programming, while working with strings, one of the most important things you should consider is the idea behind the null terminating character, which is found at the end of every string.

The Null Character ('\0') is what terminates every string, it is what tells where the string ends. Although the null character might look like something insignificant now, it has tremendous use while working with strings as it enables one to manipulate string as well as is also applicable in the application of string functions that are available in the string.h header file.

Working with Strings in C

Declaring and Initializing String as an Array

Recall in the introduction when we defined string as an array of characters, so that means for us to be able to declare a string, it has to be declared like an array.

When declaring a string, before assigning a size to the string, we have to consider the null terminating character which always marks the end of the string.

Another thing to note is that we can declare and initialize it either as a string constant or as a character constant.

To initialize it as a string constant, there will be no need to indicate the null character as the end, as the compiler will automatically do that at the point of compilation, but it is also important to consider it when giving the array holding the string its size.

To initialize it as a character constant, you will need to add the terminating character at the end of the string.

Here is a code showing an example of declaring and initializing strings in C, using both methods:

#include <stdio.h>

int main(void)
{
    char full_name[14] = "Gideon Bature";
    char blog_name[14] = {'G', 'i', 'd', 'e', 'o', 'n', ' ',
                            'B', 'a', 't', 'u', 'r', 'e', '\0'};
}

In the code above, the full_name was declared using an array. After which a string constant style was used to initialize it. For the string constant style, just the string inside a double quotation. Here just as was mentioned earlier, there is no need for us to indicate a null terminating character at the end, as the compiler will automatically do that for us at the point of compilation.

For the blog_name, which was also declared as an array, a character constant style is instead used here. For the character constant, the whole characters which are each inside a single quotation should be enclosed inside a curly braces. And the null character is also placed at the end part of the array.

Both full_name and blog_name were both given a size of 14, that is with an extra 1 space for the null terminating character. As the string "Gideon Bature" has 13 characters, including the space that is between them.

Declaring and Initializing String as a Pointer

Just as we were able to declare and initialize a string as an array, we can also declare and initialize a string as a pointer. More on Pointers here.

I know you might be wondering how possible it is, well if you look at the concept behind arrays and pointers, they have so much in common, especially when it has to do with the memory of the computer. You can check more on this here.

To declare and initialize a string using a pointer, you will have to declare a pointer to char since char is the data type of string, and then initialize the pointer with the string.

Using the example for that of the array, we are going to have:

#include <stdio.h>

int main(void)
{
    char *full_name = "Gideon Bature";
}

In the code above, full_name is declared as a pointer to a char, and the value of this pointer is the string "Gideon Bature".

What this means is that the full_name is a pointer variable that is pointing to the location of the string "Gideon Bature" in memory, thus as a result of that, the string "Gideon Bature" can be accessed through that pointer.

Accessing Characters in a String Using Array Method

Accessing characters in a string is in no way different from accessing the elements of an array. Since we are interested in accessing just the individual characters, I will be using the putchar function that is available in the standard input and output header file (stdio.h).

Since accessing the characters is similar to accessing the elements of an array, then it means we can also apply the same method, which is the method of indexing.

If you are not familiar with this, you can check my article on arrays that cover accessing the elements of an array.

The indexing starts from 0 with the first character at index 0, the second character at index 1 and like that until the null terminating character is reached.

Let us apply it to the example we have used above:

#include <stdio.h>

int main(void)
{
    char full_name[14] = "Gideon Bature";
    char blog_name[14] = {'G', 'i', 'd', 'e', 'o', 'n', ' ',
                            'B', 'a', 't', 'u', 'r', 'e', '\0'};

    putchar(full_name[0]); // gives the character @ index 0 which is 'G'
    putchar('\n');         // adds a new line
    putchar(blog_name[2]); // gives the character @ index 2 which is 'd'
    putchar('\n');         // adds a new line
}

This is how one can access each character using the index method.

To access all the characters, one can then use a while-loop or a for-loop with the logic of strings ends/terminates with a null character ('\0'). to access all the characters and print each of them to standard output.

#include <stdio.h>

int main(void)
{
    int i, j = 0;

    char full_name[14] = "Gideon Bature";
    char blog_name[14] = {'G', 'i', 'd', 'e', 'o', 'n', ' ',
                            'B', 'a', 't', 'u', 'r', 'e', '\0'};

    // for-loop to access all characters of full_name, displaying them to standard output
    for (i = 0; full_name[i] != '\0'; i++)
    {
        putchar(full_name[i]);
    }

    putchar('\n'); // adds new line

    // while-loop to access all cheacters of blog_name, displaying them to standard output
    while (blog_name[j] != '\0')
    {
        putchar(blog_name[j]);
        j++;
    }
    putchar('\n'); // adds new line
}

For the code above, a for-loop is used to access all the characters of the full_name array, printing them to the standard output.

And a while-loop is used to access all the characters of the blog_name array, also printing them to standard output.

Accessing Characters in a String Using Pointer Method

Using a pointer to access the characters of an array involves the use of pointer arithmetic, which will be explained in more detail in another article on the blog.

Recall that a pointer points to the location of a variable in memory. Thus, since Strings are arrays of characters, that means that a pointer to a string gives the base location of the string, which in this case is the character of the string at index 0, which is the first character of the string.

Thus for us to access other characters of the string, since we know the address of the first character, it will become easier to access the rest of the characters since we have learned that the elements of an array are stored in memory in a contiguous memory block, that a memory block large enough to accommodate all the elements of the array is given to the array, and each element of the array takes one memory location after the other all following themselves.

The memory address here is just an illustration of the continuous nature of the memory location of the elements of an array, in this case, the difference between the memory address is just one, and it was made so because each character has a size of one byte on the compiler, other data types like int and float have more depending on whether it is a 32 bits or 64 bits machine.

Now we can use a pointer here to access each of their memory address and then dereference the pointer to get what is stored in that particular memory address. That is how we can access each of the characters in the array. So as we keep adding one to the pointer, it moves to the next memory address that is in the same block of memory of the array.

#include <stdio.h>

int main(void)
{
    int i;

    char *full_name = "Gideon Bature";

    // for-loop to access all characters of full_name,
    // displaying them to standard output
    for (i = 0; *full_name != '\0'; i++)
    {
        putchar(*full_name);
        full_name++;
    }
    putchar('\n'); // adds new line
}
#include <stdio.h>

int main(void)
{
    int i;

    char *full_name = "Gideon Bature";

    // while-loop to access all characters of full_name,
    // displaying them to standard output
    while (*full_name != '\0')
    {
        putchar(*full_name);
        full_name++;
    }
    putchar('\n'); // adds new line
}

This is how to make use of the pointer to access all characters of a string, the two codes above are the same, the only difference is in their loops, while the first one makes use of the for-loop, the second one makes use of the while-loop, but the results are the same.

Conclusion

This covers the basics for strings, there are still some other things like string manipulation where you make use of various functions that are contained in the string.h header file functions like strlen, strcmp, strcpy, strchr, strstr and strcat etc. to find the length of a string, compare strings, copy strings, search for a character in a string, search for a string in a string and join two different strings together etc.

One can also apply some advanced concepts like multi-dimensional arrays, the pointer of a pointer etc.

Thank you for reading. You can connect with me on Twitter and LinkedIn.