GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — C++ string - working with strings in C++ C++ stringlast modified January 9, 2023 C++ string tutorial shows how to work with strings in C++.
A string is a sequence of characters. C++ has the The characters in a string literal must be enclosed between double quotation marks. C++ string access characters
To access the characters of a string, we can use the access.cpp
#include <iostream>
using std::cout;
using std::endl;
using std::string;
int main() {
string msg = "an old falcon";
cout << msg.at(4) << endl;
cout << msg[5] << endl;
cout << msg.front() << endl;
cout << msg.back() << endl;
return 0;
}
In the example, we access characters of a string. $ ./access l d a C++ string concatenate
The concat.cpp
#include <iostream>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main() {
string name, msg;
cout << "Enter your name: ";
getline(cin, name);
msg = "Hello " + name + "!";
cout << msg << endl;
return 0;
}
With the $ ./concat Enter your name: Jan Hello Jan! C++ string to int
The str2int.cpp
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
using std::stoi;
int main () {
string str1 = "12";
string str2 = "18.97";
string str3 = "4 foxes";
int val1 = stoi(str1);
int val2 = stoi(str2);
int val3 = stoi(str3);
cout << val1 << endl;
cout << val2 << endl;
cout << val3 << endl;
return 0;
}
We convert three strings to integers. $ ./str2int 12 18 4 C++ string modifyC++ has several methods to modify strings. modify.cpp
#include <iostream>
using std::string;
using std::cout;
using std::endl;
int main() {
string msg = "an old";
msg.append(" falcon");
cout << msg << endl;
msg.push_back('.');
cout << msg << endl;
msg.pop_back();
cout << msg << endl;
msg.erase(0, 3);
cout << msg << endl;
msg.insert(4, "gray ");
cout << msg << endl;
msg.replace(9, 6, "eagle");
cout << msg << endl;
msg.clear();
cout << msg.size() << endl;
return 0;
}
In the example, the initial string is modified with $ ./modify an old falcon an old falcon. an old falcon old falcon old gray falcon old gray eagle 0 C++ string compare
Strings are compared with the comparing.cpp
#include <iostream>
using std::string;
using std::cout;
using std::endl;
int main() {
string word1 = "blue";
string word2 = "blues";
if (word1.compare(word2) == 0) {
cout << "words are equal" << endl;
} else {
cout << "words are not equal" << endl;
}
if (word1.compare(0, 4, word2, 0, 4) == 0) {
cout << "words are equal" << endl;
} else {
cout << "words are not equal" << endl;
}
return 0;
}
We compare two words. In the second case, we specify the range of characters to compare. $ ./comparing words are not equal words are equal C++ substring
The substring.cpp
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
int main() {
string word = "an old falcon";
cout << word.substr(0, 2) << endl;
cout << word.substr(3, 3) << endl;
cout << word.substr(7, 6) << endl;
return 0;
}
We get three substrings of the initial string. $ ./substring an old falcon C++ string loopWe can use while and for loops to go over a string. looping.cpp
#include <iostream>
using std::cout;
using std::endl;
using std::string;
int main() {
string msg = "an old falcon";
int i = 0;
while (i < msg.size()) {
cout << msg[i] << " ";
i++;
}
cout << endl;
for (const auto &c : msg) {
cout << c << " ";
}
cout << endl;
for (auto it = msg.begin(); it != msg.end(); it++) {
cout << *it << " ";
}
cout << endl;
for (string::size_type i = 0; i < msg.size(); i++) {
std::cout << msg.at(i) << " ";
}
cout << endl;
return 0;
}
We loop over a string and print its characters. We use the classic while and for loops and the for-range loop. $ ./looping a n o l d f a l c o n a n o l d f a l c o n a n o l d f a l c o n a n o l d f a l c o n C++ string find/rfind
The finding.cpp
#include <iostream>
using std::string;
using std::cout;
using std::endl;
int main() {
string text = "I saw a red fox yesterday; a red old fox.";
int pos1 = text.find("fox");
int pos2 = text.rfind("fox");
int pos3 = text.find("fox", 15);
cout << pos1 << endl;
cout << pos2 << endl;
cout << pos3 << endl;
return 0;
}
We use the int pos3 = text.find("fox", 15);
The overloaded $ ./finding 12 37 37 C++ read fileIn the following example, we read a text file. words.txt
wind sky blue water falcon rock wood cup cloud war We have a list of words in the file. read_file.cpp
#include <iostream>
#include <fstream>
#include <string>
using std::string;
using std::cout;
using std::cerr;
using std::endl;
using std::getline;
using std::ifstream;
int main() {
ifstream filename("words.txt");
if (filename.is_open()) {
string line;
while (getline(filename, line)) {
cout << line << endl;
}
filename.close();
} else {
cerr << "Unable to open file";
}
return 0;
}
We read a file line by line and print each line to the console. $ ./read_file wind sky blue water falcon rock wood cup cloud war C++ string starts_with
The starts_with.cpp
#include <iostream>
#include <fstream>
using std::string;
using std::cout;
using std::cerr;
using std::endl;
using std::getline;
using std::ifstream;
int main() {
ifstream filename("words.txt");
if (filename.is_open()) {
string line;
while (getline(filename, line)) {
if (line.starts_with('w')) {
cout << line << endl;
}
}
filename.close();
} else {
cerr << "Unable to open file";
}
return 0;
}
We read the words from the $ ./starts_with wind water wood war C++ string ends_with
The ends_with.cpp
#include <iostream>
#include <sstream>
using std::string;
using std::cout;
using std::endl;
using std::getline;
using std::istringstream;
int main() {
string words = "wind\nsky\blue\nwater\nfalcon\nrock\nwood\ncup\ncloud\nwar";
istringstream data(words);
for (string line; getline(data, line);) {
if (line.ends_with('d')) {
cout << line << endl;
}
}
return 0;
}
We have a large string of words delimited by newline characters. We transform
the string to a stream and read the words with $ ./ends_with wind wood cloud In this article, we have worked with strings in C++. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |