Strings in C++ and How to Create them?
Last Updated :
12 Jul, 2025
Strings in C++ are used to store text or sequences 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:
1. 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 the 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 the string "Geeks" in C++.
Example:
C++
// 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;
}
2. 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 a 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++
// 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
Similar Reads
Convert String to size_t in C++ To convert String to size_t in C++ we will use stringstream, It associates a string object with a stream allowing you to read from the string as if it were a stream (like cin). We must include the stream header file in order to use stringstream. When parsing input, the stringstream class comes in qu
1 min read
How to split a string in C/C++, Python and Java? Splitting a string by some delimiter is a very common task. For example, we have a comma-separated list of items from a file and we want individual items in an array. Almost all programming languages, provide a function split a string by some delimiter. In C: // Splits str[] according to given delim
7 min read
stringstream in C++ and its Applications A stringstream associates a string object with a stream allowing you to read from the string as if it were a stream (like cin). To use stringstream, we need to include sstream header file. The stringstream class is extremely useful in parsing input. Basic methods are:clear()- To clear the stream.str
3 min read
How to Convert Vector of int into String? In C++, there are numerous cases where a vector of integers is needs to be converted into a string. In this article, we will learn the different methods to convert the vector of int into string in C++.The simplest method to convert the vector of int into string is by creating a stringstream and addi
3 min read
Different ways to copy a string in C/C++ Copying a string is a common operation in C/C++ used to create a duplicate copy of the original string. In this article, we will see how to copy strings in C/C++. Methods to Copy a String in C/C++1. Using strcpy() We can use the inbuilt function strcpy() from <string.h> header file to copy one
15+ min read
How to Iterate through a String word by word in C++ Given a String comprising of many words separated by space, the task is to iterate over these words of the string in C++.Example: Input: str = "GeeksforGeeks is a computer science portal for Geeks"Output: GeeksforGeeks is a computer science portal for Geeks Input: str = "Geeks for Geeks"Output: Geek
3 min read