A Handy Data Type
What about strings? The C programming language keeps data quantities pretty simple. However it can be challenging to write programs if you don't have a way to manage string or text data. String/Text data is really just a series of characters. However, even though that sounds simple, strings are used for names, addresses, input prompts, output displays, and so on. What you will study later on is that strings are a special case of a character array with a couple of supporting conditions. However, this topic will present strings in a way that helps you use them without having to know what is under the hood for the time being.
Creating a string. The first thing to learn about is how to create a string for use in your program. As mentioned, strings in C -- which are also known as C-Style strings -- are actually an array of characters. Thus, to create a string in a C program, you must create an array of characters. Again, you will learn much more about arrays later but for the time being, here is how to create a character array:
char name[ STD_STR_LEN ];
The char is the data type, the name is the identifier, and the block with STD_STR_LEN tells the program how many characters the string can hold. You will never use literals for any string and you will commonly use constants such as STD_STR_LEN or LG_STR_LEN or SM_STR_LEN depending on the size of the string you need.
There is more to discuss here later on when you are studying arrays but for now, this will get the job done.
Assigning, or copying, a string. Once a C-Style string has been created, the next thing to know is how to assign a string from one array to the other. This is conducted as follows:
strcpy( destinationString, sourceString );
To start with, the function is called strcpy, short for "string copy". Then in the function parameters, the destination string is always placed first, and the source string is always placed second. This means that whatever is in the source string will be placed in the destination string by this function. If the source string contains "Shelly Savalas" and the second string contains "Bill Smith", the destination string will contain "Shelly Savalas" once the function has completed, and note that "Bill Smith" will be overwritten and no longer exist in the string.
Concatenating, or appending, a string. Sometimes it is handy to be able to add one string on to the end of another, a process called concatenation. If you have a string called firstName that contains "Roger", and a string called lastName that holds " Ramjet" (note the space prior to the last name in the string quotes), the function strcat can be used to combine them as follows:
strcat( firstName, lastName );
After this call to strcat, the string firstName will contain "Roger Ramjet".
Finding the length of a string. While there are other utilities that can be used with C-Style strings, there is only one remaining operation for purposes of getting started with strings. The strlen function finds the length of a string and returns it as an integer. It is as simple as the following:
stringLength = strlen( firstName );
If the string held "Roger Ramjet"" from the previous operation, the value of stringLength would be twelve (i.e., the number of characters, including the space, in the string).
Using the C-Style string tools. As mentioned, there are a quite a few more operations that can be conducted with C-Style strings but the ones provided here will be enough for getting started with string operations. The one remaining thing that is needed is to know that these tools are found in the string.h library, so this must be included in any programs that manage strings.
Watch this video to see string management operations in action.