In C++, there are two ways to implement strings: character arrays and character pointers. Here we only discuss character arrays.
In this page, all character arrays are assumed to be treated as strings.
The following is an example of character array declaration:
C++ version | Equivalent Turbo Pascal version |
char s[10]; | var s: string[10]; |
In C++, there is a null character (with ASCII 0, indicated by '\0' in C++) at the end of a string. Therefore, if you want to have a character array which holds at most n characters, it would be better if the array size is n + 1 (the last one is used for holding the null character).
We can initialise a character array during declaration. For example:
char s[] = "Hello"; | is equivalent to | char s[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; |
We can also initialise a character array in the middle of the program. However, we have to use a string function called strcpy. To use this function, the preprocessor directive "#include <string.h>" must appear near the top of the program.
In order to use the following functions, the preprocessor directive "#include <string.h>" must appear near the top of the program.
Common string functions | Meaning |
strcpy(s, t) | copy string t to string s. |
strncpy(s, t, n) | copy at most n characters of string t to string s. ('\0' may not be automatically appended if the length of t >= n.) |
strcat(s, t) | concatenate string t to the end of string s. |
strncat(s, t, n) | concatenate at most n characters of string t to string s; terminate s with '\0'. |
strlen(s) | return the length of string s. |
strcmp(s, t) | compare string s to string t; return a negative value if s < t, zero if s = t, or a positive value if s > t. |
strncmp(s, t, n) | compare at most n characters of string s to string t; return a negative value if s < t, zero if s = t, or a positive value if s > t. |
Sample program:
#include <string.h> #include <iostream.h> main() { char a[] = "C++ programming", b[50], c[50], d[50]; int i; cout << "a contains " << strlen(a) << " characters\n"; // 15 characters strcpy(b, "Welcome to this program!"); // b is initialised strncpy(c, b, 11); // "Welcome to " is stored in c // The following statement is necessary because // strncpy does not automatically append '\0' at the end of the string c[11] = '\0'; strcpy(d, c); // "Welcome to " is stored in d strncat(c, a, 3); // "Welcome to C++" is stored in c strcat(d, a); // "Welcome to C++ programming" is stored in d // b: "Welcome to this program!" // c: "Welcome to C++" // d: "Welcome to C++ programming" cout << "b: " << b << "\nc: " << c << "\nd: " << d << endl; // Here we can see that b > d if (strcmp(b, d) > 0) cout << "b > d\n"; else if (strcmp(b, d) == 0) cout << "b = d\n"; else cout << "b < d\n"; for (i = 0; !strncmp(b, d, i); i++) ; // the loop body is empty // The above for loop is exited when strncmp(b, d, i) == 0 cout << "b and d differ at character " << i << "." << endl; return 0; } |
Sample output: |
a contains 15 characters b: Welcome to this program! c: Welcome to C++ d: Welcome to C++ programming b > d b and d differ at character 12. |