| Strings of Characters. | ||
In C++ there is no specific elemental variable type to store strings of characters. In order to fulfill this feature we can use arrays of type char, which are successions of char elements. Remember that this data type (char) is the one used to store a single character, for that reason arrays of them are generally used to make strings of single characters.
For example, the following array (or string of characters):
char jenny [20];can store a string up to 20 characters long. You may imagine it thus:
This maximum size of 20 characters is not required to always be fully used. For example, jenny could store at some moment in a program either the string of characters "Hello" or the string "Merry christmas". Therefore, since the array of characters can store shorter strings than its total length, a convention has been reached to end the valid content of a string with a null character, whose constant can be written 0 or '\0'.
We could represent jenny (an array of 20 elements of type char) storing the strings of characters "Hello" and "Merry Christmas" in the following way:
Notice how after the valid content a null character ('\0') it is included in order to indicate the end of the string. The panels in gray color represent indeterminate values.
Initialization of strings
Because strings of characters are ordinary arrays they fulfill all their same rules. For example, if we want to initialize a string of characters with predetermined values we can do it just like any other array:char mystring[] = { 'H', 'e', 'l', 'l', 'o', '\0' };In this case we would have declared a string of characters (array) of 6 elements of type char initialized with the characters that compose Hello plus a null character '\0'.
Nevertheless, strings of characters have an additional way to initialize their values: using constant strings.
In the expressions we have used in examples in previous chapters constants that represented entire strings of characters have already appeared several times. These are specified enclosed between double quotes ("), for example:
"the result is: "is a constant string that we have probably used on some occasion.
Unlike single quotes (') which specify single character constants, double quotes (") are constants that specify a succession of characters. Strings enclosed between double quotes always have a null character ('\0') automatically appended at the end.
Therefore we could initialize the string mystring with values by either of these two ways:
char mystring [] = { 'H', 'e', 'l', 'l', 'o', '\0' };In both cases the array or string of characters mystring is declared with a size of 6 characters (elements of type char): the 5 characters that compose Hello plus a final null character ('\0') which specifies the end of the string and that, in the second case, when using double quotes (") it is automatically appended.
char mystring [] = "Hello";
Before going further, notice that the assignation of multiple constants like double-quoted constants (") to arrays are only valid when initializing the array, that is, at the moment when declared. Expressions within the code like:
mystring = "Hello";are not valid for arrays, like neither would be:
mystring[] = "Hello";
mystring = { 'H', 'e', 'l', 'l', 'o', '\0' };So remember: We can "assign" a multiple constant to an Array only at the moment of initializing it. The reason will be more comprehensible when you know a bit more about pointers, since then it will be clarified that an array is simply a constant pointer pointing to an allocated block of memory. And because of this constantnes, the array itself can not be assigned any value, but we can assing values to each of the elements of the array.
The moment of initializing an Array it is a special case, since it is not an assignation, although the same equal sign (=) is used. Anyway, always have the rule previously underlined present.
Assigning values to strings
Since the lvalue of an assignation can only be an element of an array and not the entire array, it would be valid to assign a string of characters to an array of char using a method like this:mystring[0] = 'H';But as you may think, this does not seem to be a very practical method. Generally for assigning values to an array, and more specifically to a string of characters, a series of functions like strcpy are used.strcpy (string copy) is defined in the cstring (string.h) library and can be called the following way:
mystring[1] = 'e';
mystring[2] = 'l';
mystring[3] = 'l';
mystring[4] = 'o';
mystring[5] = '\0';
strcpy (string1, string2);This does copy the content of string2 into string1. string2 can be either an array, a pointer, or a constant string, so the following line would be a valid way to assign the constant string "Hello" to mystring:
strcpy (mystring, "Hello");For example:
// setting value to string
#include <iostream.h>
#include <string.h>
int main ()
{
char szMyName [20];
strcpy (szMyName,"J. Soulie");
cout << szMyName;
return 0;
}
| J. Soulie |
Although we can always write a simple function like the following setstring with the same operation as cstring's strcpy:
// setting value to string
#include <iostream.h>
void setstring (char szOut [], char szIn [])
{
int n=0;
do {
szOut[n] = szIn[n];
} while (szIn[n++] != '\0');
}
int main ()
{
char szMyName [20];
setstring (szMyName,"J. Soulie");
cout << szMyName;
return 0;
}
| J. Soulie |
When cin is used with strings of characters it is usually used with its getline method, that can be called following this prototype:
cin.getline ( char buffer[], int length, char delimiter = ' \n');where buffer is the address of where to store the input (like an array, for example), length is the maximum length of the buffer (the size of the array) and delimiter is the character used to determine the end of the user input, which by default - if we do not include that parameter - will be the newline character ('\n').
The following example repeats whatever you type on your keyboard. It is quite simple but serves as an example of how you can use cin.getline with strings:
// cin with strings
#include <iostream.h>
int main ()
{
char mybuffer [100];
cout << "What's your name? ";
cin.getline (mybuffer,100);
cout << "Hello " << mybuffer << ".\n";
cout << "Which is your favourite team? ";
cin.getline (mybuffer,100);
cout << "I like " << mybuffer << " too.\n";
return 0;
}
| What's your name? Juan Hello Juan. Which is your favourite team? Inter Milan I like Inter Milan too. |
If you remember the section about communication through the console, you will remember that we used the extraction operator (>>) to receive data directly from the standard input. This method can also be used instead of cin.getline with strings of characters. For example, in our program, when we requested an input from the user we could have written:
cin >> mybuffer;this would work, but this method has the following limitations that cin.getline has not:
- It can only receive single words (no complete sentences) since this method uses as a delimiter any occurrence of a blank character, including spaces, tabulators, newlines and carriage returns.
- It is not allowed to specify a size for the buffer. That makes your program unstable in case the user input is longer than the array that will host it.
Converting strings to other types
Due to that a string may contain representations of other data types like numbers, it might be useful to translate that content to a variable of a numeric type. For example, a string may contain "1977", but this is a sequence of 5 chars not so easily convertable to a single integer data type. The cstdlib (stdlib.h) library provides three useful functions for this purpose:- atoi: converts string to int type.
- atol: converts string to long type.
- atof: converts string to float type.
// cin and ato* functions
#include <iostream.h>
#include <stdlib.h>
int main ()
{
char mybuffer [100];
float price;
int quantity;
cout << "Enter price: ";
cin.getline (mybuffer,100);
price = atof (mybuffer);
cout << "Enter quantity: ";
cin.getline (mybuffer,100);
quantity = atoi (mybuffer);
cout << "Total price: " << price*quantity;
return 0;
}
| Enter price: 2.75 Enter quantity: 21 Total price: 57.75 |
Functions to manipulate strings
The cstring library (string.h) defines many functions to perform manipulation operations with C-like strings (like already explained strcpy). Here you have a brief look at the most usual:- strcat: char* strcat (char* dest, const char* src);
- Appends src string at the end of dest string. Returns dest.
- strcmp: int strcmp (const char* string1, const char* string2);
- Compares strings string1 and string2. Returns 0 is both strings are equal.
- strcpy: char* strcpy (char* dest, const char* src);
- Copies the content of src to dest. Returns dest.
- strlen: size_t strlen (const char* string);
- Returns the length of string.

0 comments:
Post a Comment