Split-Function-In-C++
INTRODUCTION
Any language that uses coding must support interacting with textual information. Regarding coding considerations, this is occasionally necessary for distinguishing textual information. Many computer languages have the split () method, which may break a group of characters into numerous components. There isn't an embedded technique in C++ called split () when dividing text; however, there are several other methods of performing the exact operation using C++, like utilizing the getline() operation, the strtok() operation, the find () operation, the erase () operation, etc. The preceding lesson described how you can utilize these methods for dividing texts in C++.
Let us discuss the three methods in brief. The three methods are:
- getline ()
- Strtok ()
- find ()
SPLITTING STRING BY UTILIZING getline () METHOD:
When a specific divider, as well as the separator, appears, this getline() method is employed for reading letters throughout a string and a data file until it finds that and subsequently stores every parsed text toward a different string parameter. The method continues to work unless the entire contents of the document or text have been processed. Following is a list that describes this function's syntax.
Syntax:
istream& getline(istream& is, string& str, char delim)
In this case, the initial argument, stream, indicates how the letters shall be retrieved. Its retrieved data shall be stored in a variable with a string as the second argument. The semicolon used when extracting the text has been specified using the final choice.
Use the getline() method to split a text according to the space divider by creating a C++ document containing the subsequent content. Spaces are being utilized to serve as a divider for the text composed of several characters that were allotted inside a property. The retrieved items will be kept in a vector property that was recently specified. The following action was printing every single number within the corresponding vector matrix using the "for" iteration.
The program is as follows:
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
int main ()
{
std:: string strData = "Splitiing in C++";
const char separator = ' ';
Std:: vector<std:: string> outputArray;
std:stringstream streamData(strData);
std::string val;
while (std::getline(streamData, val, separator)) {
outputArray.push_back(val);
}
std::cout << "The given string is:" << strData << std::endl;
std::cout <<
"\nThe string after splitting the following space is:" <<
std::endl;
for (auto &val: output array) {
std::cout << val << std::endl;
}
return 0;
}
OUTPUT:
![SPLIT FUNCTION IN C++](https://static.tutorialandexample.com/cpp/split-function-in-cpp1.png)
SPLITTING STRING BY UTILIZING strtok () METHOD:
The strtok() method could be applied to divide a piece of text by representing the portion that makes up the text that uses a delimiter. When a subsequent token already exists, that provides a pointer for it; otherwise, a NULL value will be returned. To use this method, the string.h header file must be present. Retrieving every divided item from the text within the loop will be necessary. The numerical data that will be translated is contained in the initial parameter, and the boundary that will be utilized for creating the token is contained in the second argument. Following is a list that describes this function's terminology.
Syntax:
char *ptr = strtok( str, delim)
The code is as good as follows:
Whereas delete signifies the symbol and the separator, we will need to begin looking for within the text, while string represents your string itself. It could be a comma (,), a space (), a hyphen (-), etc.
The program is:
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char str [100];
cout << "The string you want to split is: " <<endl;
cin.getline(str, 100);
char *ptr;
ptr = strtok(str, ", ");
cout << " Split string using strtok() function: " << endl;
while (ptr != NULL)
{
cout << ptr << endl;
ptr = strtok (NULL, ", ");
}
return 0;}
OUTPUT:
![SPLIT FUNCTION IN C++](https://static.tutorialandexample.com/cpp/split-function-in-cpp2.png)
SPLITTING STRING BY UTILIZING find () METHOD:
The C++ find() and erase() methods may be used to separate the text and test the application of each of the locate() or erase() methods for splitting a string's value depending on a particular delimiter; build a C++ document containing the code listed below. The locate() method determines the separator status, while the erase() method removes the separator before storing the object's result. The process may be continued until every character has been successfully analyzed. The rectangular array's contents are then displayed.
The program is as follows:
#include <iostream>
#include <string>
#include <vector>
int main(){
std::string string data = "Hello, my name is Bhavika Pundru";
std::string separator = "/-/";
std::vector<std::string> country{};
int position;
std::string outstr, token;
while ((position = string data.find(separator)) != std::string::npos) {
token = stringData.substr(0, position);
country.push_back(token.erase(0, token.find_first_not_of(" ")));
string data.erase(0, position + separator.length());
}
for (const auto &outstr : country) {
std::cout << outstr << std::endl;
}
std::cout << stringData.erase(0, string data.find_first_not_of(" ")) << std::endl;
return 0;
}
OUTPUT:
![SPLIT FUNCTION IN C++](https://static.tutorialandexample.com/cpp/split-function-in-cpp3.png)