Detailed Explanation of Strings in C++

In C++, strings are objects of the std::string class, representing sequences of characters. We can perform many operations on strings, such as concatenation, comparison, conversion, etc.

C++ String Example

Let’s look at a simple example of a C++ string.

#include <iostream>using namespace std;
int main() {   string s1 = "Hello";   char ch[] = { 'C', '+', '+' };   string s2 = string(ch);   cout << s1 << endl;   cout << s2 << endl;   return 0;}

Output:

Hello
C++
👇 Click to Claim 👇
👉 C Language Knowledge Resource Collection

C++ String Comparison Example

Let’s look at a simple example of comparing strings using the strcmp() function.

#include <iostream>#include <cstring>using namespace std;
int main() {   char key[] = "mango";   char buffer[50];   do {       cout << "What is my favorite fruit? ";       cin >> buffer;  } while (strcmp(key, buffer) != 0);   cout << "Answer is correct!!" << endl;   return 0;}

Output:

What is my favorite fruit? apple
What is my favorite fruit? banana
What is my favorite fruit? mango
Answer is correct!!

C++ String Concatenation Example

Let’s look at a simple example of concatenating strings using the strcat() function.

#include <iostream>#include <cstring>using namespace std;
int main() {   char key[25], buffer[25];   cout << "Enter the key string: ";   cin.getline(key, 25);   cout << "Enter the buffer string: ";   cin.getline(buffer, 25);   strcat(key, buffer);   cout << "Key = " << key << endl;   cout << "Buffer = " << buffer << endl;   return 0;}

Output:

Enter the key string: Welcome to
Enter the buffer string:  C++ Programming.
Key = Welcome to C++ Programming.
Buffer =  C++ Programming.

C++ String Copy Example

Let’s look at a simple example of copying strings using the strcpy() function.

#include <iostream>#include <cstring>using namespace std;
int main() {   char key[25], buffer[25];   cout << "Enter the key string: ";   cin.getline(key, 25);   strcpy(buffer, key);   cout << "Key = " << key << endl;   cout << "Buffer = " << buffer << endl;   return 0;}

Output:

Enter the key string: C++ Tutorial
Key = C++ Tutorial
Buffer = C++ Tutorial

C++ String Length Example

Let’s look at a simple example of calculating string length using the strlen() function.

#include <iostream>#include <cstring>using namespace std;
int main() {    char ary[] = "Welcome to C++ Programming";    cout << "Length of String = " << strlen(ary) << endl;    return 0;}

Output:

Length of String = 26

C++ String Functions

Function Description
int compare(const string& str) Used to compare two string objects.
int length() Used to find the length of a string.
void swap(string& str) Used to swap the values of two string objects.
string substr(int pos,int n) Creates a new string object containing n characters.
int size() void resize(int n) Used to adjust the length of the string to n characters.
string& replace(int pos,int len,string& str) Replaces a portion of the string starting from position pos and spanning len characters with str.
string& append(const string& str) Adds new characters to the end of another string object.
char& at(int pos) Used to access a single character at the specified position pos.
int find(string& str,int pos,int n) Used to find the string specified in the parameters.
int find_first_of(string& str,int pos,int n) Used to find the first occurrence of the specified sequence.
int find_first_not_of(string& str,int pos,int n) Used to search for the first character that does not match any character specified in the string.
int find_last_of(string& str,int pos,int n) Used to search for the last character of the specified sequence.
int find_last_not_of(string& str,int pos) Used to find the last character that does not match the specified sequence.
string& insert() Inserts new characters before the specified position pos.
int max_size() Finds the maximum length of the string.
void push_back(char ch) Adds a new character to the end of the string.
void pop_back() Deletes the last character of the string.
string& assign() Assigns a new value to the string.
int copy(string& str) Copies the contents of the string to another string.
char& back() Returns a reference to the last character.
Iterator begin() Returns a reference to the first character.
int capacity() Returns the allocated space of the string.
const_iterator cbegin() Points to the first element of the string.
const_iterator cend() Points to the last element of the string.
void clear() Removes all elements from the string.
const_reverse_iterator crbegin() Points to the last character of the string.
const_char* data() Copies the characters of the string to an array.
bool empty() Checks if the string is empty.
string& erase() Deletes the specified character.
char& front() Returns a reference to the first character.
string& operator+=() Adds a new character to the end of the string.
string& operator=() Assigns a new value to the string.
char operator Retrieves the character at the specified position pos.
int rfind() Finds the last occurrence of the string.
iterator end() References the last character of the string.
reverse_iterator rend() Points to the first character of the string.
void shrink_to_fit() Reduces the allocated space to equal the size of the string.
char* c_str() Returns a pointer to an array containing a null-terminated sequence of characters.
const_reverse_iterator crend() References the first character of the string.
reverse_iterator rbegin() References the last character of the string.
void reserve(int len) Requests a change in capacity.
allocator_type get_allocator(); x Returns the allocated object associated with the string.

Detailed Explanation of Strings in C++

Popular Recommendations
  • CLion Tutorial – Gradle in CLion

  • C Language Algorithm – “Subset II” Algorithm Problem

  • C++ Tutorial – Detailed Explanation of Namespaces in C++

Leave a Comment