How To Split a String in C++? 5 Methods Explained with Codes

How To Split a String in C++?

Are you curious to know “How to Split a String in C++“? Learning how to split a string is a fundamental skill that every programmer should possess. In this article, we will discuss the different methods that will make splitting strings a breeze. Apart from that if you have any doubts or issues related to other C++ concepts then you can use CodingZapโ€™s best C++ assignment help, services.

Strings in C++ differ from those in C, providing additional features and functionality.

In C++, strings are represented by the std::string class from the <string> library. These strings offer dynamic memory management, support for various string operations, and seamless integration with the STL library.

The major difference between strings in C and C++ is that in C, strings are represented as an array of characters and terminated by a null character (โ€˜\0โ€™), whereas the strings in C++ are objects of the std::string class and donโ€™t require the null character to represent its termination.

Letโ€™s look at string declaration and initialization in C++

 

Code Explained For String Declaration and Initialization in C++:

 

#include <iostream> using namespace std;

int main(void)
{
// Directly initializing strings in C++ string str1 = "First string";
string str2("Second string");

string str3;
// Taking string input cin>>str3;

cout << str1 << endl; cout << str2 << endl; cout << str3 << endl;

// C style strings
char str4[] = {'H', 'e', 'l', 'l', 'o', '\0'}; char str5[] = "C style strings";
cout << str4 << endl << str5;

}
Output

First string

Second string

InputString

Hello
C style strings

 

What is the Need to Split Strings? Read Below

 

Splitting strings is a common requirement when dealing with textual data. It allows us to extract meaningful information, manipulate data effectively, and process inputs from different sources. Consider scenarios such as extracting words from a sentence, parsing data from a CSV file, or simply tokenizing the user input.

 

What is the Need to Split Strings? Read Below

Tokenizing is the process of breaking down user-provided input into smaller units called tokens, which are meaningful components of the input that can be processed or analyzed individually. A common method to split strings helps us with all of these applications effectively. Also, If you’re to know the need for exception handling in C, then you can check out our article.

The character or the sequence of characters that we use to mark the boundaries between different parts of the string is called a delimiter. The substrings on either part of a delimiter are considered two different tokens. Common delimiters are commas, spaces, colons, etc.

Let’s examine some examples of input and output when splitting strings:

  • Using space as the delimiter:

INPUT: “Iโ€™m learning how to split strings in C++”
OUTPUT:
Iโ€™m learning how
to split
strings in C++

  • Using a comma as the delimiter:

INPUT: “Ayushman Singh,23,Male,180 cm,Software Developer”

OUTPUT:
Ayushman Singh 23
Male 180 cm
Software Developer

 

What are the Methods to Split Strings in C++?

 

Although C++ does not have a built-in โ€˜split()โ€™ function like Java, it offers a plethora of alternatives to achieve the same outcome. If you’re looking to know the methods to compare strings in Java or compare Python stringsย then you can check out our article. Let’s examine several approaches to splitting strings:

1. Using only stringstream:

A stream in programming refers to a continuous flow of data elements that can be read from or written to by the computer. It is an abstraction that represents a flow of data, similar to a stream of water flowing continuously, upon which we can perform operations in a sequential manner.

The stringstream class in C++ is part of the <sstream> header and provides us with a convenient way to treat strings as input/output streams. It allows us to read data from a string and write data to a string as if they were input and output streams, respectively.

An example of splitting the space-separated values in a stringstream using the โ€˜<<โ€™ operator is as follows:

 

Code of Splitting the Space-Separated Values in a Stringstream Using the โ€˜<<โ€™ operator:

 

#include <bits/stdc++.h> using namespace std;

int main()
{
// Input string
string str = "Ayushman 23 180.10 Developer";

// Creating a stringstream from our input string stringstream ss(str);

// Declaring variables that will store values from the stream string name;
int age; double height;
string designation;

// extracting the name and age one at a time ss >> name;
ss >> age;

// extracting the height and designation in a single line ss >> height >> designation;

// printing the extracted values cout << name << endl;
cout << age << endl; cout << height << endl;
cout << designation << endl;

return 0;
}
Output:

Ayushman 23
180.1
Developer

To use stringstream, we create an object of the stringstream class and initialize it with a string. This creates a stream-like object that can be used to perform operations on the string.

The โ€˜<<โ€™ operator is used to extract the individual data elements from a stringstream separated by a space. It uses space as the default delimiter.

In the above code, we can see that our input had elements of different data types, which were read efficiently into their respective data types by using the โ€˜>>โ€™ operator. Thus the stringstream provides an efficient method to read data.

However, we cannot use the above code if we wish to declare some other character as our delimiter, such as a comma. We will hence use the stringstream with the getline() function, which allows us to declare a custom delimiter.

2. Using StringStream with getline() function

The โ€˜getline()โ€™ function is a versatile function that is used to extract data from a stream, such as a stringstream or cin.
Its basic syntax is as follows:

SYNTAX: getline(stream, str, delimiter)

It reads the characters from the โ€˜streamโ€™ until it encounters the โ€˜delimiterโ€™ specified by the user or a newline character (โ€˜\nโ€™). It then stores the extracted characters into the string specified by the user (โ€˜strโ€™).

The following code is an example of how to use stringstream with the getline() function to split a string and extract data elements based on any user-defined delimiter.

 

Code of How to Use Stringstream With the getline() function to Split a String and Extract Data Elements Based on Any User-defined Delimiter:

 

#include <bits/stdc++.h> using namespace std;

// Method to split the string into individual tokens using stringstream and getline vector<string> splitString(string input, char delimiter)
{
// Create a stream from the input string stringstream ss(input);

string temp; // weโ€™ll store individual elements in temp vector<string> tokens;

// Iterate through the stringstream and extract tokens based on the delimiter while (getline(ss, temp, delimiter)) {
tokens.push_back(temp);
}

return tokens;
}

int main() {
string s1 = "Iโ€™m learning how to split strings in C++";
string s2 = "Ayushman Singh,23,Male,180 cm,Software Developer";

// Split s1 using space as delimiter vector<string> tokens1 = splitString(s1, ' ');

// Split s2 using comma as delimiter vector<string> tokens2 = splitString(s2, ',');

// Print the tokens for s1
for (auto& token : tokens1) { cout << token << endl;
}
cout << " " << endl;

// Print the tokens for s2
for (auto& token : tokens2) { cout << token << endl;
}

return 0;
}
Output:

Iโ€™m learning how
to split
strings in C++

Ayushman Singh 23
Male 180 cm
Software Developer

3. Using find() and substr() functions:

We can also use the find() and substr() functions in conjunction to extract data from our string and split it around any delimiter of choice.

The find() function, a function of the std::string class, is used to find a specified character or substring in any given string and returns the first occurrence of the substring after the starting position if found, else it returns string::npos to depict that the substring is not found.
SYNTAX: find(substring, pos);

The substr() function is also a member function of the std::string class and is used to extract a substring from a given string. It takes the start index and the length of the substring to be extracted as its parameters and returns the required substring.
SYNTAX: substr(pos, len);

Following is the code to split a string and extract the elements using find() and substr() functions

 

Code To Split a String and Extract the Elements Using Find() and substr() Functions:

 

#include <bits/stdc++.h> using namespace std;

// Method to split a string into individual tokens using find and substr vector<string> splitString(string input, char delimiter)
{
vector<string> tokens;

// The start index of our first token int start = 0;

// Index of the first occurrence of the delimiter int end = input.find(delimiter);

// Iterate through the string and extract tokens based on the delimiter while (end != string::npos)
{
// push the extracted substring into the vector tokens.push_back(input.substr(start, end - start));

// update the indexes to the next substring start = end + 1;
end = input.find(delimiter, start);
}
tokens.push_back(input.substr(start)); return tokens;
}

int main() {
string s1 = "Iโ€™m learning how to split strings in C++";
string s2 = "Ayushman Singh,23,Male,180 cm,Software Developer";

// Split s1 using space as delimiter vector<string> tokens1 = splitString(s1, ' ');

// Split s2 using comma as delimiter vector<string> tokens2 = splitString(s2, ',');

// Print the tokens for s1
for (auto& token : tokens1) { cout << token << endl;
}

cout << " " << endl;

// Print the tokens for s2
for (auto& token : tokens2) { cout << token << endl;
}

return 0;
}
Output:

Iโ€™m learning how
to split
strings in C++

Ayushman Singh 23
Male 180 cm
Software Developer

4. Using strtok() function:

The strtok() function in C++ is a string tokenization function that is part of the C standard library. It is used to split a string into a sequence of tokens based on a specified delimiter character or string.
SYNTAX: strtok(str, delimiter);

Here, both the string โ€˜strโ€™ and the delimiter are C-styled strings. String โ€˜strโ€™ is the string that needs to be split, and the delimiter defines the set boundaries.
The function strtok() returns a pointer to the first token found in the string. If no token is found, it returns a null pointer.

Here is a sample code that uses the strtok() function to split the strings in C++

 

Code that Uses the strtok() function to Split the Strings in C++:

 

#include <bits/stdc++.h> using namespace std;

// Function to split a string into tokens using strtok

vector<string> splitString(string input, const char* delimiter)
{
vector<string> tokens;

// get the first element from the string
char* token = strtok(const_cast<char*>(input.c_str()), delimiter);

// Tokenize the input string using strtok while (token != NULL) {
tokens.push_back(string(token)); token = strtok(NULL, delimiter);
}

return tokens;
}

int main() {
string s1 = "Iโ€™m learning how to split strings in C++";
string s2 = "Ayushman Singh,23,Male,180 cm,Software Developer";

// Split s1 using space as the delimiter vector<string> tokens1 = splitString(s1, " ");

// Split s2 using comma as the delimiter vector<string> tokens2 = splitString(s2, ",");

// Print the tokens for s1 for (auto& t : tokens1) { cout << t << endl;
}

cout << " " << endl;

// Print the tokens for s2 for (auto& t : tokens2) { cout << t << endl;
}

return 0;
}
Output:ย 

Iโ€™m learning how
to split
strings in C++

Ayushman Singh 23
Male 180 cm
Software Developer

It’s important to note that strtok() converts C++ style strings to C-styled strings. It thus modifies the original string by inserting null terminators. Additionally, it is not
thread-safe, meaning it can have unexpected behavior in a multi-threaded environment.

5. Creating Your Own Custom Function:

In addition to the provided inbuilt methods, we can also create our own custom algorithm to split strings in C++.
There can be many ways to split strings using a custom function. One of them is as follows:

 

Code to Split Strings Using a Custom Function:

 

#include <bits/stdc++.h> using namespace std;

// Splitting a string into tokens using a custom function vector<string> splitString(const string& input, char delimiter)
{
vector<string> tokens;
string temp; // Temporary string to hold the characters of each token

// Iterate through each character of the input string for (char c : input) {
if (c != delimiter) {
temp += c; // Append the character to the temporary string
} else {
tokens.push_back(temp); // Add the completed token to the vector temp.clear(); // Clear the temporary string for the next token
}
}

// Add the last token if the temporary string is not empty if (!temp.empty()) {
tokens.push_back(temp);
}

return tokens; // Return the vector of tokens
}

int main() {
string s1 = "Iโ€™m learning how to split strings in C++";
string s2 = "Ayushman Singh,23,Male,180 cm,Software Developer";




// Split s1 using space as the delimiter vector<string> tokens1 = splitString(s1, ' ');

// Split s2 using comma as the delimiter vector<string> tokens2 = splitString(s2, ',');

// Print the tokens for s1
for (string& token : tokens1) { cout << token << endl;
}

cout << " " << endl;

// Print the tokens for s2
for (string& token : tokens2) { cout << token << endl;
}

return 0;
}
Output:ย 

Iโ€™m learning how
to split
strings in C++

Ayushman Singh 23
Male 180 cm
Software Developer

In the above code, we iterate through each character of the input string. If a character is not equal to the delimiter, we append it to the temp string. This step allows us to construct each token by progressively adding characters.

Upon encountering a delimiter character, we consider the current token complete. We then add the content of the temp string to the tokens vector. Following that, we clear the temp string in preparation for the next token.

After processing all the characters in the input string, if there is any remaining content in the temp string, we include it as the last token, provided it is not empty.

 

Conclusion:

 

In this article, we explored various methods to split strings in C++. We started by understanding the need for splitting strings and how it helps in extracting meaningful information from textual data. And discussed common delimiters used to mark boundaries between different parts of a string, such as spaces and commas.

We then delved into different methods to split strings, including the usage of stringstream with the extraction operator (<<), stringstream with getline(), find() and substr() functions, and the strtok() function. Each method was explained with sample code, highlighting their syntax and illustrating their functionality.

By utilizing these different methods, we were able to efficiently split strings and extract tokens based on the specified delimiters. The choice of method depends on the specific requirements of the application and the nature of the input string. With these techniques at our disposal, we can confidently handle split strings in C++ and process textual data effectively.

If you’re struggling with you’re C homework then you can hire the best programming experts at CodingZap.

So, hope you have liked this piece of article. Share your thoughts in the comments section and let us know if we can improve more.

Leave a Comment

Your email address will not be published. Required fields are marked *