C++ Strings

Programs often need to work with text, such as names, messages, addresses, and sentences.

In C++, a string is used to store a sequence of characters.

For example:

string name = "Rahul";

Here, name stores the text "Rahul".

Creating a String

To use string, include the <string> library.

#include <iostream>
#include <string>
using namespace std;

int main() {
    string name = "Rahul";

    cout << name;

    return 0;
}

Output:

Rahul

A string can contain letters, numbers, spaces, and special characters.

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

Taking String Input

You can use cin to take a single word from the user.

#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;

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

    cout << "Hello, " << name;

    return 0;
}

If the user enters:

Rahul

Output:

Hello, Rahul

However, cin stops reading when it finds a space.

For example, if the user enters:

Rahul Kumar

only Rahul will be stored.

Using getline()

To read a complete line, including spaces, use getline().

#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;

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

    cout << "Hello, " << name;

    return 0;
}

If the user enters:

Rahul Kumar

Output:

Hello, Rahul Kumar

Finding the Length of a String

You can use the length() function to find the number of characters in a string.

#include <iostream>
#include <string>
using namespace std;

int main() {
    string name = "Rahul";

    cout << name.length();

    return 0;
}

Output:

5

The word Rahul contains 5 characters.

Accessing String Characters

You can access individual characters using an index.

Just like arrays, string indexing starts from 0.

Index:    0   1   2   3   4
Character R   a   h   u   l

Example:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string name = "Rahul";

    cout << name[0] << "\n";
    cout << name[2];

    return 0;
}

Output:

R
h

You can also change a character:

string name = "Rahul";

name[0] = 'S';

Now the string becomes:

Sahul

Joining Strings

You can join strings using the + operator.

#include <iostream>
#include <string>
using namespace std;

int main() {
    string firstName = "Rahul";
    string lastName = "Kumar";

    string fullName = firstName + " " + lastName;

    cout << fullName;

    return 0;
}

Output:

Rahul Kumar

Here, " " adds a space between the two names.

Comparing Strings

You can compare strings using comparison operators.

#include <iostream>
#include <string>
using namespace std;

int main() {
    string password = "12345";

    if (password == "12345") {
        cout << "Correct password";
    }

    return 0;
}

Output:

Correct password

This can be useful when checking names, passwords, choices, and other text values.

Looping Through a String

You can use a loop to access each character.

#include <iostream>
#include <string>
using namespace std;

int main() {
    string word = "Hello";

    for (int i = 0; i < word.length(); i++) {
        cout << word[i] << "\n";
    }

    return 0;
}

Output:

H
e
l
l
o

A Simple Example

Let’s create a program that asks for a person’s name and prints the number of characters in it.

#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;

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

    cout << "Your name is: " << name << "\n";
    cout << "Number of characters: " << name.length();

    return 0;
}

If the user enters:

Anjali

Output:

Your name is: Anjali
Number of characters: 6

Key Points to Remember

  • A string stores text.
  • Include <string> when working with strings.
  • cin reads a single word.
  • getline() reads a complete line, including spaces.
  • length() returns the number of characters.
  • String indexing starts from 0.
  • The + operator can join strings.
  • Strings can be compared using operators such as ==.

Mini Quiz

0%

C++ PROGRAMMING - QUIZ 9

1 / 5

Q1. Which library is required to use string in C++?

2 / 5

Q2. What will be stored in name after the user enters Rahul Kumar using cin >> name;?

3 / 5

Q3. Which function is used to read a complete line of text, including spaces?

4 / 5

Q4. Which function is used to find the number of characters in a string?

5 / 5

Q5. What is the main difference between cin and getline() when taking string input?

Your score is

The average score is 0%

0%

Mini Project

Mini Project: Name Analyzer

Question:

Write a C++ program that takes a person’s name and displays information about it.

  • Take the person’s full name using getline().
  • Display the entered name.
  • Display the number of characters using length().
  • Display each character of the name on a separate line.
  • Use a for loop to access each character.
  • Use appropriate variable names.
View Answer Code
#include <iostream>
#include <string>
using namespace std;

int main() {

    string name;

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

    cout << "\nName: " << name;
    cout << "\nNumber of characters: " << name.length();

    cout << "\nCharacters:\n";

    for (int i = 0; i < name.length(); i++) {
        cout << name[i] << "\n";
    }

    return 0;
}

What’s Next?

In the next chapter, we will learn about C++ Functions and see how to organize code into reusable blocks.


Frequently Asked Questions (FAQs)

Q1. What are C++ Strings?

C++ Strings are used to store and work with text, such as names, messages, addresses, and sentences.

Q2. How do you create a C++ string?

A C++ string can be created using the string data type. For example, string name = "Rahul"; stores the text "Rahul".

Q3. How do you take C++ string input with spaces?

C++ string input containing spaces can be taken using getline(). It reads the complete line entered by the user.

Q4. What are common C++ string functions?

Common C++ string functions include length() for finding the number of characters. C++ also provides many other string operations for working with text.

Q5. What is getline() used for in C++?

C++ getline is used to read a complete line of text, including spaces, from the input stream.

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

Scroll to Top