๐งต 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++ Strings | Introduction to string creation and usage |
๐ Loop Through a String | Iterate through each character in a string |
๐ String Length | Get the number of characters in a string |
โ String Concatenation | Join two or more strings together |
๐ String Comparison | Compare 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 :