A String in C/C++ is an array of characters that ends with the null character ‘0’. Characters are stored in a string. The C programming language does not have a string data type; instead, it employs a character array.
Character array is a collection of char data types in a sequential order. A string is a collection of characters displayed as a single item that is terminated with the null character ‘0’.
How to Create a String in C++ With Examples
To create a string in C++, we have two options. The first is to initialize string as the character of arrays. The second method is to use C++’s string class, which allows us to create strings from objects of that class.
Types of Strings in C++
There are two types of strings in C++:
- String class in C++
- Character String in C-Style
String class in C++
Strings are one of the most commonly used data types in C++ programming. Characters are stored in double quotations in string type variables. They are objects of the string class, which is included in the C++ standard library.
It is usual practice to include a header file in the code when using string variables. The following is the syntax for adding the C++ Strings header file:

The following is the syntax for initializing a string variable:

The code to initialize the name variable, for example, is as follows:

The program can also accept user input in the form of strings. The following code displays a string supplied by the user.

The following is the output of the aforementioned code after compilation:

The getline function is used to enter a full sentence as an input. The getline function accepts two parameters. The keyword cin is the first argument, and the string variable is the second. The getline function has the following syntax:

The code below demonstrates how to utilize the getline function to extract sentences from a user’s input as a string.

The following is the output of the aforementioned code after compilation:

C-Style Character String
A character string in C-style is essentially an array of character types with characters enclosed in double-quotes. It’s a one-dimensional array with the null character ‘0’ at the end. This array is a character more than the entire number of characters.
The following is the syntax for initializing a character string:

Strings can be defined in a variety of ways by a programmer. The following is the second syntax for initializing a string array:

The following is the third syntax for initializing an array:

In the code below, for example, three-character arrays are initialized in different ways but have the same results.

Example Code

The following is the output of the aforementioned code after compilation:

It’s worth noting that all three statements get the same result.
Carrying Out Basic Operations
String Length: The length() function is used to determine the string’s length.
Example Code:

Accessing the Characters of a String: A string is an array of characters, as we all know. As a result, the index number inside the square brackets [] can be used to access a specific character in a string. These represent the character’s current position.
Example Code:

Output: l //Which is the string’s third element
Altering string characters: To modify a string character, use index numbers inside square brackets.
Example Code:

Output: Food //G replaced with F
Input a string from a User:
Example Code:

Output:

Note: The cin function only reads a single word, not a string of words. As a result, only the first word will be shown.
Concatenating a String: To combine strings, use the ‘+’ operator.
Example Code:

Output:
