Strings in C++ and How to Create them?

13 Nov, 2019
Strings in C++ are used to store text or sequence of characters. In C++ strings can be stored in one of the two following ways:
C style string (using characters)
String class
Each of the above methods is discussed below:
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
C style string: In C, strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’. In C, the string is actually represented as an array of characters terminated by a null string. Therefore the size of the character array is always one more than that of the number of characters in the actual string. This thing continues to be supported in C++ too. The C++ compiler automatically sets “\0” at the end of the string, during initialization of the array.Initializing a String in C++:1. char str[] = "Geeks";
2. char str[6] = "Geeks";
3. char str[] = {'G', 'e', 'e', 'k', 's', '\0'};
4. char str[6] = {'G', 'e', 'e', 'k', 's', '\0'};
Below is the memory representation of a string “Geeks” in C++.Let’s look at some examples to better understand the string representation in C++, using C style:// C++ program to demonstrate// Strings using C style  #include <iostream>using namespace std;  int main(){      // Declare and initialize string    char str[] = "Geeks";      // Print string    cout << str;      return 0;}Output:Geeks
Standard String representation and String Class: In C++, one can directly store the collection of characters or text in a string variable, surrounded by double-quotes. C++ provides string class, which supports various operations like copying strings, concatenating strings etc.Initializing string in C++:1. string str1 = "Geeks";
2. string str2 = "Welcome to GeeksforGeeks!";
Example:// C++ program to demonstrate String// using Standard String representation  #include <iostream>#include <string>using namespace std;  int main(){      // Declare and initialize the string    string str1 = "Welcome to GeeksforGeeks!";      // Initialization by raw string    string str2("A Computer Science Portal");      // Print string    cout << str1 << endl << str2;      return 0;}Output:Welcome to GeeksforGeeks!
A Computer Science Portal

C style string: In C, strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’. In C, the string is actually represented as an array of characters terminated by a null string. Therefore the size of the character array is always one more than that of the number of characters in the actual string. This thing continues to be supported in C++ too. The C++ compiler automatically sets “\0” at the end of the string, during initialization of the array.Initializing a String in C++:1. char str[] = "Geeks";
2. char str[6] = "Geeks";
3. char str[] = {'G', 'e', 'e', 'k', 's', '\0'};
4. char str[6] = {'G', 'e', 'e', 'k', 's', '\0'};
Below is the memory representation of a string “Geeks” in C++.Let’s look at some examples to better understand the string representation in C++, using C style:// C++ program to demonstrate// Strings using C style  #include <iostream>using namespace std;  int main(){      // Declare and initialize string    char str[] = "Geeks";      // Print string    cout << str;      return 0;}Output:Geeks

Initializing a String in C++:
1. char str[] = "Geeks";
2. char str[6] = "Geeks";
3. char str[] = {'G', 'e', 'e', 'k', 's', '\0'};
4. char str[6] = {'G', 'e', 'e', 'k', 's', '\0'};

Below is the memory representation of a string “Geeks” in C++.
Let’s look at some examples to better understand the string representation in C++, using C style:
// C++ program to demonstrate// Strings using C style  #include <iostream>using namespace std;  int main(){      // Declare and initialize string    char str[] = "Geeks";      // Print string    cout << str;      return 0;}
Geeks

Standard String representation and String Class: In C++, one can directly store the collection of characters or text in a string variable, surrounded by double-quotes. C++ provides string class, which supports various operations like copying strings, concatenating strings etc.Initializing string in C++:1. string str1 = "Geeks";
2. string str2 = "Welcome to GeeksforGeeks!";
Example:// C++ program to demonstrate String// using Standard String representation  #include <iostream>#include <string>using namespace std;  int main(){      // Declare and initialize the string    string str1 = "Welcome to GeeksforGeeks!";      // Initialization by raw string    string str2("A Computer Science Portal");      // Print string    cout << str1 << endl << str2;      return 0;}Output:Welcome to GeeksforGeeks!
A Computer Science Portal

Initializing string in C++:
1. string str1 = "Geeks";
2. string str2 = "Welcome to GeeksforGeeks!";

Example:
// C++ program to demonstrate String// using Standard String representation  #include <iostream>#include <string>using namespace std;  int main(){      // Declare and initialize the string    string str1 = "Welcome to GeeksforGeeks!";      // Initialization by raw string    string str2("A Computer Science Portal");      // Print string    cout << str1 << endl << str2;      return 0;}
Welcome to GeeksforGeeks!
A Computer Science Portal

Related Articles:
C++ string class and its applications | Set 1
C++ string class and its applications | Set 2
cpp-strings
C++
cpp-strings
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.