C++ Strings and String Manipulation


What Is a String in C++?

A string in C++ is a collection of characters grouped together to represent words, sentences, or any textual data. Unlike individual characters (stored as char), a string can store multiple characters in a single variable.

In modern C++, strings are often handled using the std::string class from the library, offering numerous features for working with text easily and efficiently.


Why Strings Are Useful

  • Store names, messages, and words
  • Search, compare, and edit text
  • Build output messages dynamically
  • Concatenate or slice parts of words
  • Input and store user-provided text

Syntax of String Declaration

#include <string>  

Std::string greeting = "Hello, world!"; 
  • std::string: The string data type from the standard library
  • greeting: Variable to hold the text
  • "Hello, world!": Literal text assigned to the string

Example

#include <iostream> 
#include <string>  

int main() {     
       std::string city = "Paris";     
       std::cout << "Traveling to: " << city << std::endl;     
       return 0; 
} 

Here, "Paris" is stored inside the city variable and printed with the sentence. This allows combining fixed text and dynamic string content.


Common String Operations

OperationDescription (Unique Wording)Example
+ (Join)Combine two texts into onefirst + last
.length()Count how many characters are inside the stringname.length()
.empty()Checks if a string holds no contentphrase.empty()
.at(index)Picks a specific letter by positionword.at(2)
.substr(start, n)Cuts out a portion from the stringline.substr(0, 4)
.find("text")Searches for a pattern and returns its positionmsg.find("ok")
.replace(pos, len, "new")Substitutes a section with new contentnote.replace(0, 4, "Hi")

Example

#include <iostream> 
#include <string>  

int main() {     
      std::string fullName = "Alex Stone";      

     fullName.replace(5, 5, "Smith");  // Change "Stone" to "Smith"      

     std::cout << "Updated name: " << fullName << std::endl;     
     return 0; 
} 

In this case, the original string "Alex Stone" is updated to "Alex Smith" by swapping part of it.


Another Handy Example

#include <iostream> 
#include <string>  

int main() {     
       std::string first = "Data";     
       std::string second = "Science";   
   
       std::string result = first + " " + second;     
       std::cout << "Field: " << result << std::endl; 
     
       Return 0; 
} 

This combines two separate string variables with a space in between to create "Data Science".


Working with User Input

#include <iostream> 
#include <string>  I

nt main() {     
      std::string message;      

      std::cout << "Enter your message: ";     
      std::getline(std::cin, message);  // Accepts full line with spaces      

      std::cout << "You typed: " << message << std::endl;     
      return 0; 
} 

Summary

C++ strings give you the ability to hold and manipulate textual data with simplicity and speed. From joining sentences and replacing words to slicing parts and searching within them, the std::string class enables developers to handle language-based data smoothly. Unlike character arrays, these strings are more flexible, easier to work with, and provide a wide set of built-in tools to manage text efficiently.


Prefer Learning by Watching?

Watch these YouTube tutorials to understand C++ Tutorial visually:

What You'll Learn:
  • 📌 How Strings Work in C++ (and how to use them)
  • 📌 Basics Of Using The string Type | C++ Tutorial
Previous Next