In C programming language ASCII codes are used as in char arrays to store texts in ASCII mode. You can use char arrays in both C and C++, they are faster in operations and they have less memory usage. In s modern way, strings are useful for storing texts and they are defined in the string library. A string
class contains a collection of characters surrounded by double quotes as we used in char arrays.
A string variable can be assigned as below,
1 2 3 |
string str = "This is a string example"; |
Here is the full C++ example that shows how to print out a string to screen,
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> #include <string> int main(int argc, char** argv) { std::string str = "This is a string example"; std::cout << str; return 0; } |
and the output will be,
1 2 3 |
This is a string example |
Table of Contents
1. Reading and Modifying Characters of a string
strings are used like char arrays, you can reach each character by using [ ] brackets and the index number. See example below
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> #include <string> int main(int argc, char** argv) { std::string str = "This is a string example"; std::cout << str[1]; return 0; } |
or you can use std in namespace so you don’t need to use std:: in each std commands as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <string> using namespace std; int main(int argc, char** argv) { string str = "This is a string example"; cout << str[1]; return 0; } |
output will be second character as below,
1 2 3 |
h |
Note that index numbers in C and C++ starts with 0, and str[0] is ‘T’ character. We can modify character of a string as below,
1 2 3 4 |
std::string str = "This is a string example"; str[1]='X'; |
String Concatenation
strings are easy to operate with them in many ways, while we use strcat(), strcpy() functions in char arrays.
1 2 3 4 5 6 |
std::string name = "Jim"; std::string surname = "White"; std::string nameandsurname = name+" "+surname; |
strings are classes with many features, this is why they are preferred in modern C++. For example you can add another string by using its append() function.
1 2 3 4 5 6 |
std::string str1 = "ABCD"; std::string str2 = "EFGH"; std::string str = str1.append(str2); |
this is same as
1 2 3 |
std::string str = str1 + str2; |
Length of a string
Length of string, number of characters can be obtained by using it’s length() command as below,
1 2 3 4 |
string str = "ABCDEF"; cout << "The length of string =" << str.length(); |
Size of a string
Size of string is size of string in the memory, if characters has 1 byte in compiler option this will equal to length, if characters has 2 bytes it will have double size of its length.
1 2 3 4 |
string str = "ABCDEF"; cout << "The size of string =" << str.size(); |
Note that, in C++ Builder both string and String are used in same ways. In the last C++ Builder versions Strings are UnicodeString, supports globally and operations are explained before in Unicode Strings in C++ On Windows