C++ Strings

When you work with numbers in C++, you can use data types like int and float. But what if you want to store a person’s name, an address, or a simple message?

For this type of data, you need a string. A string is a sequence of characters used to store text.

For example:

"Hello"
"Rahul"
"Welcome to C++"

All of these are strings because they contain text.

String in C++

In C++, there are different ways to work with text. For beginners, the easiest and most commonly used way is the string type provided by the C++ Standard Library.

To use it, include the string header file:

#include <string>

You can then create a string variable like this:

string name = "Rahul";

Here, name is the variable and "Rahul" is the text stored inside it.

You can display the value using cout:

cout << name;

Output

Rahul

A C++ String can contain letters, numbers, spaces, and special characters.

For example:

string message = "Welcome to C++!";

Here, the entire text is stored as one string.

Creating a String

You can create a string without giving it a value immediately.

string name;

Later, you can assign a value to it:

name = "Aman";

You can also create a string with a complete sentence:

string message = "C++ is easy to learn";

The spaces between the words are also part of the string.

Taking String Input

You can take a string as input using cin.

string name;

cout << "Enter your name: ";
cin >> name;

cout << "Hello " << name;

If the user enters:

Aman

the output will be:

Hello Aman

However, there is one thing to remember. cin stops reading when it finds a space.

For example, if the user enters:

Aman Kumar

only Aman will be stored in the string.

When you want to accept a complete sentence or a name containing spaces, use getline().

string name;

cout << "Enter your full name: ";
getline(cin, name);

cout << "Hello " << name;

Now if the user enters:

Aman Kumar

the complete name will be stored.

C++ Strings and Characters

A string can contain many characters, while a char stores only one character.

For example:

char grade = 'A';
string name = "Aman";

Notice the difference in quotation marks.

A character uses single quotes:

'A'

A string uses double quotes:

"Aman"

This small difference is important when working with text in C++.

Why Use Strings?

Strings are used whenever a program needs to work with text. For example, a student management program may store student names, while a login system may store usernames and messages.

You can also perform different operations on strings, such as finding their length, joining two strings, comparing them, or accessing individual characters.

C++ provides many C++ String Functions and features that make these operations easier. We will cover them in the upcoming string topics.

So, whenever you need to store text in a C++ program, a string is usually the right choice. Once you understand how a String Class in C++ works, you can easily move on to creating, reading, changing, and processing strings.


Frequently Asked Questions (FAQs)

Q1. What is a string in C++?

A string in C++ is a sequence of characters used to store text such as names, words, and sentences. C++ supports both C-style strings and the std::string class.

Q2. What is the difference between C-style strings and the string class in C++?

C-style strings use character arrays terminated by a null character (\0), while the String Class in C++ provides a simpler and more flexible way to work with text.

Q3. How do you take string input in C++?

You can use cin to read a single word or getline() to read a complete line, including spaces.

Q4. How do you find the length of a string in C++?

The length of a std::string can be found using the length() or size() function, which returns the number of characters in the string.

Q5. How do you concatenate two strings in C++?

Two strings can be joined using the + operator or the append() function. This operation is called string concatenation.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top