shri shankaracharya technical campus
Concept of String
A string is a sequence of characters used to represent text.
In C++, strings can be represented in two main ways:
1. C-style strings – character arrays (char str[20] = "Hello";)
2. C++ standard string class – std::string from <string> header.
The Standard C++ String Class
The C++ string class (in the Standard Template Library - STL) provides powerful functions
for:
Storing and manipulating text data.
Automatic memory management.
Built-in operators and methods for concatenation, comparison, etc.
Header File:
#include <string>
using namespace std;
Declaration Example:
string str1 = "Hello";
string str2("World");
Common Operations on Strings
(a) length() – Get number of characters
Definition:
Returns the number of characters in the string (same as size()).
🔹 Syntax:
int len = str.length();
🔹 Examples:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "Hello";
cout << s1.length() << endl; // 1
dr. Somesh Kumar Dewangan Department of cse 1
shri shankaracharya technical campus
string s2 = "Programming";
cout << s2.length() << endl; // 2
string s3 = "C++ String";
cout << s3.length() << endl; // 3
string s4 = "";
cout << s4.length() << endl; // 4
string s5 = "ChatGPT";
cout << "Length: " << s5.length() << endl; // 5
}
(b) capacity() – Get storage capacity
Definition:
Returns the current allocated storage capacity for the string (≥ length).
🔹 Syntax:
cout << str.capacity();
🔹 Examples:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "Hello";
cout << s1.capacity() << endl; // 1
string s2 = "This is a test string.";
cout << s2.capacity() << endl; // 2
s2.reserve(50);
cout << s2.capacity() << endl; // 3
string s3 = "Short";
cout << s3.capacity() << endl; // 4
string s4;
s4 = "Dynamic Capacity Example";
dr. Somesh Kumar Dewangan Department of cse 2
shri shankaracharya technical campus
cout << s4.capacity() << endl; // 5
}
(c) resize() – Change size of string
Definition:
Resizes the string to contain the specified number of characters.
🔹 Syntax:
str.resize(new_size);
🔹 Examples:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "HelloWorld";
s1.resize(5);
cout << s1 << endl; // 1: Hello
string s2 = "Hi";
s2.resize(5, '*');
cout << s2 << endl; // 2: Hi***
string s3 = "C++";
s3.resize(10, '#');
cout << s3 << endl; // 3: C++#######
string s4 = "ResizeExample";
s4.resize(6);
cout << s4 << endl; // 4: Resize
string s5 = "Data";
s5.resize(8, '@');
cout << s5 << endl; // 5: Data@@@@
}
dr. Somesh Kumar Dewangan Department of cse 3
shri shankaracharya technical campus
(d) getline() – Read a full line of text
Definition:
Reads a line of text from input (including spaces) into a string.
🔹 Syntax:
getline(cin, str);
🔹 Examples:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1;
cout << "Enter a sentence: ";
getline(cin, s1); // 1
cout << s1 << endl;
string s2;
cout << "Enter your name: ";
getline(cin, s2); // 2
cout << "Name: " << s2 << endl;
string s3;
cout << "Enter address: ";
getline(cin, s3); // 3
cout << s3 << endl;
string s4;
cout << "Enter full statement: ";
getline(cin, s4); // 4
cout << s4 << endl;
string s5;
cout << "Enter remarks: ";
getline(cin, s5); // 5
dr. Somesh Kumar Dewangan Department of cse 4
shri shankaracharya technical campus
cout << "Remarks: " << s5 << endl;
}
(e) begin() and end() – Iterators
Definition:
begin() returns iterator to the first character.
end() returns iterator to one past the last character.
🔹 Syntax:
for (auto it = str.begin(); it != str.end(); ++it)
🔹 Examples:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "Hello";
for (auto it = s1.begin(); it != s1.end(); ++it)
cout << *it; // 1
cout << endl;
string s2 = "C++";
for (char c : s2) cout << c; // 2
cout << endl;
string s3 = "Iterator";
for (auto i = s3.begin(); i != s3.end(); i++)
cout << *i << "-"; // 3
cout << endl;
string s4 = "Example";
auto it = s4.begin();
cout << "First char: " << *it << endl; // 4
string s5 = "EndTest";
dr. Somesh Kumar Dewangan Department of cse 5
shri shankaracharya technical campus
cout << "Last char: " << *(s5.end() - 1) << endl; // 5
}
(f) copy() – Copy part of a string to a character array
Definition:
Copies a portion of the string into a character array.
🔹 Syntax:
str.copy(destination_array, length, position);
🔹 Examples:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "HelloWorld";
char ch1[6];
s1.copy(ch1, 5, 0);
ch1[5] = '\0';
cout << ch1 << endl; // 1: Hello
string s2 = "Programming";
char ch2[5];
s2.copy(ch2, 4, 3);
ch2[4] = '\0';
cout << ch2 << endl; // 2: gram
string s3 = "StringCopyExample";
char ch3[8];
s3.copy(ch3, 7, 6);
ch3[7] = '\0';
cout << ch3 << endl; // 3
string s4 = "C++Language";
char ch4[4];
dr. Somesh Kumar Dewangan Department of cse 6
shri shankaracharya technical campus
s4.copy(ch4, 3, 2);
ch4[3] = '\0';
cout << ch4 << endl; // 4
string s5 = "ChatGPT";
char ch5[8];
s5.copy(ch5, s5.length(), 0);
ch5[s5.length()] = '\0';
cout << ch5 << endl; // 5
}
(g) swap() – Exchange contents of two strings
Definition:
Swaps the contents of two strings instantly.
🔹 Syntax:
str1.swap(str2);
🔹 Examples:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "Apple", s2 = "Banana";
s1.swap(s2);
cout << s1 << " " << s2 << endl; // 1
string a = "First", b = "Second";
a.swap(b);
cout << a << " " << b << endl; // 2
string x = "Hello", y = "World";
swap(x, y);
cout << x << " " << y << endl; // 3
string str1 = "Data", str2 = "Science";
dr. Somesh Kumar Dewangan Department of cse 7
shri shankaracharya technical campus
str1.swap(str2);
cout << str1 << " " << str2 << endl; // 4
string p = "C++", q = "Python";
p.swap(q);
cout << p << " " << q << endl; // 5
}
dr. Somesh Kumar Dewangan Department of cse 8