Book
Collection
Click for Table of Contents
© 2025 by Rance D. Necaise
C Primer for Python Programmers
Copyright © 2025
Rance D. Necaise

8.3 Working with Strings

Although the C programming language does not provide a built-in data type for declaring string variables, it does provide a collection of functions for working with strings as part of the C standard library. In addition, since C strings are stored in character arrays, we are able to access and manipulate the individual character elements. For example, consider the string definition

  1. char fruit[20] = "Apple";

which is stored in the character array as

and suppose we want to print the string to the terminal, one character per line, as shown below:

a
p
p
l
e
Note
Note

The C programming language does not provide an iterator construct similar to the for loop in Python that can be used to quickly iterate over the elements of an array. Instead, we have to write our own loops or use one of the standard string functions.

String Iteration

When iterating over the elements of a C string, you must remember the importance of the null character that terminates the string. Every character from the first position until you encounter the null character is part of the string. In the example above, the string is comprised of the first five elements (as highlighted below):

To print the characters of the string, one per line, we can iterate over the elements, one at a time, starting from the first position. The iteration continues until we encounter the null character:

  1. int i = 0;
  2. while(fruit[i] != '\0') {
  3.   printf("%c\n", fruit[i]);
  4.   i++;
  5. }

Modifying Strings

Strings in C are generally mutable unless they are defined as constant strings. That means, we can change the characters within a string by simply setting the array element to a different character. For example,

  1. fruit[0] = 'A';

which results in the modified array

We can also create a string by adding individual characters to the character array. Consider the string declarations:

  1. char first[] = "Sally";
  2. char last[] = "Roberts";

which we want to use to create a new string initials that contains the individuals initials. First, we need to create a character array to store our string

  1. char initials[5];

Remember, the array has to be large enough to store the characters that comprise the string plus the null character. Here, we only need four elements to store the characters in the string and one for the null character.

Now, we can build the string by reading the first element from each of the two arrays, first and last, and storing those into the proper elements of new array:

  1. initials[0] = first[0];
  2. initials[1] = '.';
  3. initials[2] = last[0];
  4. initials[3] = '.';

which results in

Finally, we must terminate the string with the null character, otherwise, we would just have an array of characters:

  1. initials[4] = '\0';

Having created the string, we can print it to the terminal just like any other string

  1. printf("%s\n", initials);

which produces

S.R.


Question 8.3.1

Consider the following code segment which contains a logic/run-time error:

  1. char fruit[20] = "apple";
  2. printf("%s\n", fruit[0]);

Identify the error.

The %s format specifier indicates that a string is to be printed, but we provided a character instead. Remember, the individual elements of the array are characters.

Question 8.3.2

How can the error in the previous question be corrected?

Either the %s has to be changed to %c

  1. printf("%c\n", fruit[0]);

or we should only provide the name of the character array

  1. printf("%s\n", fruit);
Question 8.3.3

Consider the array

What would be result of executing the following code segment?

  1. fruit[0] = '\0';

The array would now store the empty string since the null character is in the first position of the array.