In C++, strings are objects of the std::string class, representing sequences of characters. We can perform various operations on strings such as concatenation, comparison, or conversion, ... .
For detailed insights into strings in C++, readers are encouraged to continue reading the following article by Mytour.
Article Table of Contents:
1. Example of string in C++
2. Example of string comparison in C++
3. Example of string concatenation in C++
4. Example of string copying in C++
5. Example of string length in C++
6. String manipulation functions in C++
1. Example of string in C++
Below is an example of a string in C++:
#include
using namespace std;
int main() {
string str = 'Mytour';
char characters[] = {'C', '+', '+'};
string s2 = string(characters);
cout << str << endl;
cout << 'Hello, World!' << endl;
return 0;
The output result is formatted as:
Mytour
C++
2. Example of string comparison in C++
Refer to the simple example below to compare strings using the strcmp() function.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char keyword[] = 'Mytour';
char storage[50];
do {
cout << 'What is your favorite website?';
cin >> storage;
} while (strcmp(keyword, storage) != 0);
cout << 'Answer:' << endl;
return 0;
}
The output result is formatted as:
What is your favorite website? Mytour
What is your favorite website? 9mobi.vn
What is your favorite website? Xephang.net
Correct answer!!
3. Example of string concatenation in C++
Below is an example of string concatenation in C++ using the strcat() function:
#include <iostream>
#include <cstring>
using the standard namespace;
int main()
{
char passcode[25], temp[25];
cout < 'Enter the key string:';
cin.getline(key, 25);
cout < 'Enter the buffer string:';
cin.getline(buffer, 25);
strcat(key, buffer);
cout < 'Concatenated key=' << key << endl;
cout << ' buffer=' << buffer << ' <p>';
return 0;
}
The output result is as follows:
Enter the key string: Welcome
Enter the buffer string: Mytour.
Key = Welcome to Mytour.
Buffer = Mytour.
4. Example of string copying in C++
The following example illustrates string copying in C++ using the strcpy() function:
#include <iostream>
#include <cstring>
using the standard namespace;
int main()
{
char passcode[25], temp[25];
cout < 'Enter the key string:';
cin.getline(key, 25);
strcpy(buffer, passcode);
cout < 'key='<< passcode << endl;
cout << ' buffer='<< buffer << ' <p>';
return 0;
}
Output result:
Enter the key string: Tricks Mytour
Key = Tricks Mytour
Buffer = Tricks Mytour
5. Example of string length in C++
The following example demonstrates finding the length of a string using the strlen() function:
#include <iostream>
#include <iostream>
using the standard namespace;
int main()
{
char array[] = 'Welcome to Mytour';
cout < 'The length of the string is: ' << strlen(array) << ' <p>';
return 0;
}
Output result:
Length of the string: 24
6. String manipulation functions in C++
Below is a table listing the string manipulation functions in C++:
Function Description
int compare(const string& str) Compares objects of two strings.
int length() Finds the length of the string.
void swap(string& str) Exchanges the values of two string objects.
string substr(int pos,int n) Creates a new string object consisting of n characters.
int size() Returns the length of the string in bytes.
void resize(int n) Changes the length of the string to up to n characters.
string& replace(int pos,int len,string& str) Replaces a portion of the string starting at character position and extending len characters.
string& append(const string& str) Adds new characters to the end of another string object.
char& at(int pos) Accesses individual character at the specified position.
int find(string& str,int pos,int n) Finds the specified string in the parameter.
int find_first_of(string& str,int pos,int n) Finds the first occurrence position of the specified string.
int find_first_not_of(string& str,int pos,int n ) Searches for the first character string that does not match any characters specified in the string.
int find_last_of(string& str,int pos,int n) Searches for the last occurrence of the specified character string.
int find_last_not_of(string& str,int pos) Searches for the last character that does not match the specified string.
string& insert() Inserts a new character before the specified character at the given position.
int max_size() Finds the maximum length of the string.
void push_back(char ch) Adds a new character ch at 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 from this 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 storage capacity 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() Clears all elements of the string.
const_reverse_iterator crbegin() Points to the last character of the string.
const_char* data() Copies the characters of the string into an array.
bool empty() Checks if the string is empty or not.
string& erase() Erases all specified characters.
char& front() Returns a reference to the first character.
string& operator+=() Appends a new character at the end of the string.
string& operator=() Assigns a new value to the string.
char operator[](pos) Retrieves a character at the specified position.
int rfind() Searches for the last occurrence position of a 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 capacity to fit the size of the string.
char* c_str() Returns a pointer to an array containing the 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 the capacity member function.
allocator_type get_allocator(); Returns the allocator object associated with the string.
This article provided detailed insights into what a string is in C++. Additionally, readers can explore more articles to gain a deeper understanding of loops in C. In the next C programming tutorials on Mytour, we will delve into loops in C with specific examples and analysis.
