C++ Tutorial
Estimated reading: 4 minutes 31 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿงต C++ Strings & String Manipulation

Or Copy Link

CONTENTS
Scroll to Top