C++ Tutorial
Estimated reading: 4 minutes 465 views

C++ Strings & String Manipulation โ€“ Complete Guide for Beginners


Introduction โ€“ Why Strings Matter in C++

Strings are one of the most important data types in programming because they allow us to work with text. In C++, string manipulation is supported through both the built-in character array (char[]) and the more powerful std::string class. Whether you’re processing user input, formatting output, or parsing data, string handling is a fundamental skill.

In this guide, youโ€™ll learn:

  • How to use and manipulate strings in C++
  • String operations like length, looping, comparison, and concatenation
  • Useful functions from the C++ <string> and <cstring> libraries

Topics Covered

Topic Description
C++ StringsIntroduction to string creation and usage
Loop Through a StringIterate through each character in a string
String LengthGet the number of characters in a string
String ConcatenationJoin two or more strings together
String ComparisonCompare strings for equality or sorting
C++ String Library โ€“ <cstring>Low-level string functions for advanced operations

C++ Strings

In C++, you can define strings using either char arrays or the std::string class.

Using std::string (Recommended):

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

int main() {
    string greeting = "Hello, World!";
    cout << greeting;
    return 0;
}

Using char[] (C-style):

char greeting[] = "Hello, World!";

Use std::string for more flexibility and ease of manipulation.


C++ Loop Through a String

You can use a loop to iterate through each character of a string:

string word = "C++";

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

You can also use a range-based for loop:

for (char c : word) {
    cout << c << " ";
}

C++ String Length

The .length() or .size() function returns the number of characters:

string name = "John";
cout << "Length: " << name.length();  // Output: 4

Both .length() and .size() are interchangeable in std::string.


C++ String Concatenation

Strings can be joined using the + operator or .append() method.

string first = "Hello";
string last = "World";
string full = first + " " + last;
cout << full;  // Output: Hello World

Using .append():

first.append(" ").append(last);

C++ String Comparison

Use ==, !=, <, and > to compare strings.

string a = "Apple";
string b = "Banana";

if (a < b)
    cout << "Apple comes before Banana.";

To ignore case or perform more advanced comparison, use functions from <cstring> or algorithm.


C++ String Library โ€“ <cstring>

The C++ <cstring> header provides legacy C-style functions for character array manipulation:

Function Description
strlen(str)Returns the length of the string
strcpy(dest, src)Copies one string to another
strcmp(a, b)Compares two strings (returns 0 if equal)
strcat(a, b)Concatenates two strings

Example:

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

int main() {
    char a[20] = "Hello";
    char b[] = "World";
    strcat(a, b);
    cout << a;  // Output: HelloWorld
}

Summary โ€“ Recap & Next Steps

Key Takeaways:

  • std::string provides modern and convenient ways to handle text
  • You can loop, compare, and concatenate strings easily using built-in functions
  • Use <cstring> for low-level string functions when working with C-style arrays

C++ offers flexible tools for string handling through the Standard Library and C-string support. Understanding both methods gives you greater control when dealing with user input, text formatting, and parsing.


FAQs โ€“ C++ Strings & Manipulation

What is the difference between char[] and std::string?
char[] is a low-level array of characters, while std::string is a C++ class that simplifies string manipulation.

How do I compare two strings in C++?
Use == for comparison in std::string, or strcmp() if using C-style strings.

Can I modify individual characters in a string?
Yes. You can use str[i] = 'x'; to change a character at a specific index.

Is it necessary to include <cstring> for string manipulation?
Only if you’re working with char[]. For std::string, use <string>.

How can I concatenate strings without using +?
Use the .append() method: str1.append(str2);


Share Now :
Share

๐Ÿงต C++ Strings & String Manipulation

Or Copy Link

CONTENTS
Scroll to Top