C Language Reference for Script Programmers - Strings

Strings

Overview                                                                                                                                                                    

As mentioned earlier a string is an array of chars. The string: “Hello World” can be assigned to a variable in either of the two following ways:

char string[12] = “Hello World”;

or

charstring[] = “Hello World”;

the second will automatically create an array of 12 chars to hold the string. Why 12 chars? Well C needs some way to determine when it’s reached the end of a string, so the last char in a string needs to be ASCII code 0 ( commonly referred to as NULL ). The character that corresponds to this value is: ‘\0’. So since all strings need to end with a NULL, every string needs an extra char to hold that NULL.

string.h                                                                                                                                                                      

Working with strings is fairly impractical without the aid of the standard C library. There exist a group of functions, defined in string.h, that aid greatly in the manipulation of strings. The most common are:

char *  strcpy(char *dest, char *source);
char *  strcat(char *dest, char *source);
int     strcmp(char *string1, char *string 2);
int     strlen(char *string);

You’ll notice that the arguments to these functions are all char *’s. I won’t go too far into this, but you can pass a char array to these functions where ever you see a char * as the argument ( In most respects an array name is equivalent to a pointer to the first element in the array, see a text for more info).

strcpy
Strcpy
(read “string copy”) copies one string to another. It is very important that the destination string is large enough to receive the source string. If the destination string is too small you run the risk of corrupting data that doesn’t belong to you. Here’s an example:


#include <string.h>

char source[] = “this is a test”;
char dest[15]; 

strcpy(dest, source);

This will work since dest is 15 chars in length and source is also 15 chars in length. Notice that I don’t use the square brackets when passing dest and source to strcpy. You don’t really need to know why you don’t need to use them just remember that the array name will suffice ( if you really want to use the brackets you could have passed dest[0] and source[0] and it would mean the same thing, again you don’t need to know why right now).

strcat
Strcat
(read “string cat” or “string concatenate”) will append the source string to the end of the dest string. Again you must watch your dest size. Often you’ll initialize your strings to some safe but reasonable size. If you know that your strings will only ever contain 1 or two words a size of 64 might be reasonable:


#include <string.h>

char first[64];
char second[] = “World”; 

strcpy(first, “Hello ”);

strcat(first, second);
… 

Here we know that we’ll be adding to the size of first so we make it 64 chars in length. Then we use strcpy to copy the string “Hello “ to first. Notice that the second argument to strcpy is the actual string “Hello “ and not a variable. Refer back to our discussion on pointers and passing by value if it is unclear why this can be done (the value “Hello “ is copied to the string that is the second argument of the function). Finally we use strcat to append second to first. After this function first’s value will be “Hello World”.

Never ever ever try to assign a string to another with something like:

string1 = string2;

That is wrong wrong wrong! Use strcpy to copy the string. Never try to add two strings with the + operator like:

string1 = string 2 + string3;

or

strcpy(string1, string2 + string3);

That is also wrong. Use strcat to add two strings.

strcmp
Strcmp
(read “string compare”) is used to test whether or not two strings are the same. This function will return 0 if the strings match and will return non-zero otherwise. So you could use the following to perform some action if two strings match:


if( strncmp( string1, string2) == 0)
{
    
// perform some action
    

}

Again don’t try to use:

if ( string1 == string2 )

it won’t work. Strings are a different kind of beast and have to be treated differently. I know it seems difficult to have to use these functions instead of just using the +, == and = operators, but if you had to do it the long way (manipulating each character in the array) you’d really be hating life.

strlen
Finally there’s strlen (read “string len” or “string length”). This will return an int indicating the size of the string in chars excluding the terminating null. You’ll want to use this function to determine whether or not a destination string is large enough before using strcpy and strcat:

if( strlen(dest) >= strlen(source) )
{
    
strcpy(dest, source);
}

That should be enough to get you started with strings. For more information on string manipulation look up the following functions in a C reference:

strstr
strncmp
strncpy
strncat
strchr
strcspn

also look up pointer arithmetic as it makes string manipulation on the character level considerably quicker. For info on sorting see qsort.

Converted from CHM to HTML with chm2web Pro 2.82 (unicode)