Basic Strings
Overview
- A string is an array of characters, ending in '\0'
- While you may not operate on an entire array at once, C provides
a library of functions that allow you to think about strings as single entities.
- For strings, you must separate the space allocated in the string (which was set at allocation)
from the length of the string (which always ends at the '\0' character).
Declaring and Allocating Strings
Very soon, you will need to separate in your mind declaration from allocation. In this section,
I am only showing the syntax for declaring and allocating the string at the same time.
Declaring a variable means that you specify the name and the type.
Allocating a variable is when the space where that variable will reside is created.
For the variables we've seen so far, those two things happen at the same time. This pairing of the two is about to end.
- String variables are declared identically to character arrays. There is absolutely no difference between the two except the contents, which is covered in initialization.
- Allocated but uninitialized strings have undetermined length - they have an allocated portion, but if there is no '\0' within that allocated portion, then the length will be considered longer than the allocated portion. So be careful!
- Examples:
char str_name[size];
Initializing Strings
Initializing a variable is placing the first values into that variable.
It is important to initialize strings (at least to "") so that there is a '\0' present in the allocated space.
- Strings may be initialized when they are declared, just as any other variables. Several choices are below.
- You can use quotation marks, which will add '\0' after the last item. (str1, str2)
- You can use { } with commas, just like with arrays. (str3, str4)
- An array may be partially initialized, by providing fewer data items than the size of the array. The remaining array elements will be automatically initialized to zero. (str2, str5)
- Examples:
char str1[] = "Hello world"; // allocate 12, initialize to "Hello world"
char str2[50] = "Hello world"; // allocate 50, initialize to "Hello world"
char str3[] = {'H','e','l','l','o',' ','w','o','r','l','d', '\0'}; // allocate 12, initialize to "Hello world"
char str4[12] = {'H','e','l','l','o',' ','w','o','r','l','d', '\0'}; // allocate 12, initialize to "Hello world"
char str5[20] = "" // allocate 20, but initialize to empty string
Strings as Arrays
Strings are much like arrays, except for two constraints:
- The type inside the array is always char
- The length is determined by the location of '\0'
This changes what the loop looks like to step through the string.
Let's find the length of the string (the number of characters before '\0'):
unsigned int stringlength(char s[])
{
int length = 0;
for (length = 0; s[length] != '\0'; length++)
; // all of the work is being done in the for loop syntax!
return length;
}
Strings as Strings
While you may not operate on an entire array at once. However,
you can use the string library, provided by C. Make sure you #include <string.h>
Here are a few common ones.
Function prototype |
Functionality |
strlen(char s[]) |
Calculates number of characters before '\0' |
strncpy( char dest[], char src[], unsigned int n) |
Copies characters from source to destination. Will copy through '\0' or n characters, which is reached first. |
strcat( char dest[], char src[], unsigned int n) |
Copies characters from source to the *end* of destination (concatenation). Will copy through '\0' or n characters, which is reached first. |
strcmp(char str1[], char str2[]) |
Compares str1 and str2. If str1 < str2 (previous in abc order), return -1. Same, return 0. >, return 1 |
|
strlwr(char s[]), strupr(char s[]) |
converts string to lowercase or uppercase, respectively |
Below shows a relatively meaningless function that utilizes several of the string library functions.
#include
#include
#include
void do_some_stuff(char s1[], char s2[])
{
int s1_len = strlen(s1);
int s2_len = strlen(s2);
strncpy(s1, s2, s1_len);
strlwr(s1);
strlwr(s2);
if (strcmp(s1, s2) == 0)
printf("%s == %s\n",s1, s2);
else
printf("%s != %s\n",s1, s2);
}