🧵 C++ Strings

Soumik DastidarSoumik Dastidar
3 min read

🔤 What Is a String?

A string is a sequence of characters, like words or sentences. In C++, you can handle strings in two ways:

  • Using character arrays (char[]) — old-school, C-style

  • Using the C++ string class (std::string) — easier and more powerful

In this series, we’ll focus on std::string, as it’s beginner-friendly and more versatile.


📦 Including the String Library

To use stringYou must include its header:

#include <string>

Also include:

#include <iostream>
using namespace std;

✨ Declaring and Initializing Strings

string name = "Soumik";
string greeting;
greeting = "Hello!";

🧠 String Input and Output

📥 Input using cin (word only):

string name;
cin >> name;  // Reads until the first space

📥 Input using getline() (full line):

string fullName;
getline(cin, fullName);  // Reads the full line including spaces

📤 Output using cout:

cout << "Your name is " << name << endl;

🛠️ Common String Operations

OperationExampleResult
Lengthname.length()Number of characters
Concatenationfirst + lastCombines two strings
Substringname.substr(0, 4)First 4 characters
Access charactername[0]First character
Comparisonstr1 == str2Returns true/false

🧪 Code Example

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

int main() {
    string name;
    cout << "Enter your full name: ";
    getline(cin, name);

    cout << "Hi, " << name << "!" << endl;
    cout << "Your name has " << name.length() << " characters." << endl;
    return 0;
}

🖨️ Sample Output:

Enter your full name: Soumik Dastidar
Hi, Soumik Dastidar!
Your name has 16 characters.

🚫 Common Mistakes

❌ Using cin For full names:

cin >> fullName;  // Only captures up to first space

❌ Forgetting to include <string>:

string name = "Test";  // Error if <string> not included

✅ Best Practices

  • Prefer getline() for any input that might contain spaces.

  • Use meaningful string names like userName, not just s.

  • Avoid mixing cin and getline() directly one after another—clear the buffer first.


💼 Task for You

🔧 Task:
Ask the user for their favorite quote. Then:

  • Print the quote.

  • Print the number of characters.

  • Print the first word of the quote using substr() and find().

💡 Hint: Use quote.substr(0, quote.find(" ")) to extract the first word.


🎯 What’s Next?

Now that you know how to work with strings, it's time to move into conditions and decision-making, like if, else, and switch.

📍Next up: Part 7 – Conditional Statements in C++


✨ Follow the Series

This post is part of the C++ Zero to Hero blog series — your guide to mastering C++ from scratch. Whether you're learning for fun, school, or career prep, we've got your back—line by line, concept by concept.

0
Subscribe to my newsletter

Read articles from Soumik Dastidar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Soumik Dastidar
Soumik Dastidar

I'm Soumik—a passionate problem solver, aspiring software developer, and lifelong learner. Currently building a C++ Complete Series to help beginners master programming from scratch. I love breaking down complex concepts, automating real-world tasks, and sharing what I learn. Let's grow together, one line of code at a time.