C Language Reference for Script Programmers - 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); 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(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 … 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 … 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 if( strlen(dest) >= strlen(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 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) |