Finding the Length of a String

In the previous example, the sizeof() operator was used to determine the memory requirements of a variable. But what if you wanted to just count the number of characters in a string? For that, you can use the appropriately named strlen() function (short for string length):

num = strlen(my_string);

Note that this function does not count the terminating character that concludes every string. For example (Figure 11.5):

char my_string[] = "Larry";

printf("The string '%s' has %d
 characters.", my_string,
 strlen(my_string));

Figure 11.5. The strlen() function does not count a string's terminating character.


Despite the value returned by strlen(), my_string requires 6 bytes of memory since it contains 6 characters: L, a, r, r, y, and .

By using the strlen() function, in conjunction with some loops and arithmetic, you can calculate the actual character count and memory requirements of the arrays in ragged.c.

To determine the length of a string

1.
Open ragged.c in your text editor or IDE.

2.
Add a C preprocessor directive including the string.h header file (Script 11.2):

#include <string.h>

Script 11.2. The space requirements of the two variables are calculated using the sizeof() and strlen() functions.


To use the strlen() function, along with many of the other functions being used in this chapter, you must include the string.h header file, where that macro is defined.

3.
After the definition of the two arrays, define three integer variables:

int i;
int chapters_count = 0, ptr_count = 0;

The first variable, i, will be a for loop counter. The next two variables will be used to total up the number of characters and bytes required by the two arrays.

4.
After the original print statements, use a loop to calculate the total length of the strings:

for (i = 0; i < 5; ++i) {
     chapters_count +=
 strlen(chapters[i]);
}

Although the chapters array uses 135 bytes of memory, we'd like to determine how many characters it actually contains. The strlen() function will do this. The loop will go through each subarray of chapters, adding that string's character count to the total.

5.
Print the total string length:

printf ("The chapters array contains
 %d characters.
", chapters_count);

6.
Calculate the amount of memory being used by the pointer array and its values:

ptr_count = sizeof(chapters_ptr) +
 chapters_count + 5;

Determining the amount of actual memory being used by the pointer array requires a little math and some understanding of how C works. To start with, the sizeof(chapters_ptr) chunk will return the memory used for the pointers themselves, which we know is 20 bytes. To this we add the previously determined character count, since each character being stored requires 1 byte. Finally, 5 more bytes are added, representing the terminating character for each string, which were not counted by strlen().

7.
Print the amount of actual memory being used:

printf ("The chapters_ptr array -
 actually- uses %d bytes of
 memory.
", ptr_count);

8.
Save the file as length.c, compile, and debug as necessary.

9.
Run the application (Figure 11.6).

Figure 11.6. This example shows how a specific character array requires 135 bytes of memory to store 98 characters. Using a pointer array, those same 98 characters can be stored using only 123 bytes.


✓ Tips

  • The sizeof() function returns a value of type size_t, a special type in C. It's defined as whatever type the sizeof operator returns (which makes sense, in a circular way). Thus, size_t could be an unsigned integer, an unsigned long integer, and so forth. You can print this value using the %z modifier, so %zu represents an unsigned size_t value.

  • Given the fact that all arrays are indexed beginning at 0 and the fact that strings are terminated by a character, it's very easy to improperly refer to a string element. Use the strlen() function and C preprocessor directives to help avoid indexing mistakes.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset