Splitting a string into individual words is a common task in programming. It allows us to process and manipulate text data more efficiently. While C++ doesn't have a built-in split function, there are several methods and techniques we can use to achieve this. In this article, we will explore six different approaches to splitting strings in C++. Let's learn about all of them!
6 Methods to Split a String in C++
Here is the list of those methods which you can use to split a string into words using your own delimiter function:
- Using Temporary String
- Using stringstream API of C++
- Using strtok() Function
- Using Custom split() Function
- Using std::getline() Function
- Using find(), substr() and erase() Functions
Now, to split a string we must specify on what basis we are going to do it, here comes the delimiter. So, splitting strings is only possible with specific delimiters such as white space( ), comma(,), hyphen(-), and so on, resulting in individual words.
1) Using Temporary String
In this example, we are first taking input from the user using getline() function, and the separator variable is used as a delimiter to separate the string using commas.
Then, we are iterating to the end of the string, if we do not find our space delimiter, then we will continue adding chars to my temp string and if in between we found our delimiter then, just print that string and make it empty as shown in the below example:
2) Using stringstream API of C++
You need to know about stringstream first.
We use cin stream to take input from the user, similarly, we first initialize the stringstream's object and take the input in it using "<<" operator which allows it to read a string as a stream of words.
The most commonly used stringstream operators are as follows:
- Operator<<: pushes a string object into the stream.
- Operator>>: extracts a word from the stream.
Note: Tokenizing a string means splitting it with respect to a delimiter.
Syntax of stringstream object:
In this method, we will first create a stringstream object which will take the string and split it into words automatically. To read those words we will create a variable word and we will read all the words till my stringstream object exhaust.
3) Using strtok() Function
The strtok() function is a widely-used method to split strings in C++. The strtok() function splits the string into tokens based on the delimiter passed. The strtok() function modifies the original string on each call by inserting the NULL character (\0) at the delimiter position. This allows it to track the status of the token easily.
Here, the string is the given which we want to split, delimiter is the parameter or character based on which we separate the string. It returns a pointer to the next character tokens. It initially points to the first token of the strings.
In this method, we are first taking string as input using getline() function and then creating a pointer of type char and using strtok() function with space as a delimiter, it will give us each word. For that, we run a loop until the char pointer is not equal to NULL. In each iteration, we print the pointer.
4) Using Custom split() Function
If you prefer a more customized approach, you can create your own split() function. In this method, we are using for loop to iterate over the entire string until we find our delimiter. If found, then we will append up to that string into a vector of temp string and update the startIndex and endIndex accordingly. Here, we are defining our own custom function to split a string in C++.
5) Using std::getline() Function
Another method to split strings in C++ is by using the std:::getline() function. This function reads a string from an input stream until a delimiter character is encountered. Just as we take the input from the user using getline() function, similarly we will take the input into the sringstream using getline() function.
Here, the token saves the extracted string tokens from the original string. Below is the C++ program implementation:
6) Using find(), substr() and erase() Functions
An alternative approach to split strings in C++ is by using the find() and substr() functions. The find() function searches for a specified substring within a string, while the substr() function extracts a substring from a given position. In this method, we will use the find(), substr(), and erase() function to split the given string using our delimiter.
Note: The substr() returns a string object and size_t is an unsigned integer type. Below is the C++ program implementation :
You can also check out this article: String Interpolation.
In this article, we learned about the various methods involved to take words out of the given string or to split a string based on the delimiter passed. In competitive programming, you will find most of the time the use of this operation accordingly. By understanding these techniques, you will have the necessary knowledge to manipulate string data effectively in your C++ programs.