C++ is an amazing programming language that is widely accepted for creating applications. With this, programmers develop various projects that are high in performance. When working with C++, strings play an important role, which is a variable used to store text. You must have tried to split string by space in C++. You must be here to get some solid tips and check some methods to implement in your project. Well, we are here to provide the best assistance possible.
Strings are split when a collection of strings or sets of words are divided into a single word. Delimiters are used to split strings such as commas, a hyphen, white space, and many others. To split a string by space seems a bit complex, check out the methods below
Methods to split string by space in C++
Custom split() Method
This method is effective when it comes to splitting strings by space. Have a look at the program
#include <iostream>
#include <string>
#define max 8 // define the max string
using namespace std;
string strings[max]; // define max string
// length of the string
int len(string str)
{
int length = 0;
for (int i = 0; str[i] != '\0'; i++)
{
length++;
}
return length;
}
// create custom split() function
void split (string str, char seperator)
{
int currIndex = 0, i = 0;
int startIndex = 0, endIndex = 0;
while (i <= len(str))
{
if (str[i] == seperator || i == len(str))
{
endIndex = i;
string subStr = "";
subStr.append(str, startIndex, endIndex - startIndex);
strings[currIndex] = subStr;
currIndex += 1;
startIndex = endIndex + 1;
}
i++;
}
}
int main()
{
string str = "Program to split strings using custom split function.";
char seperator = ' '; // space
split(str, seperator);
cout <<" The split string is: ";
for (int i = 0; i < max; i++)
{
cout << "\n i : " << i << " " << strings[i];
}
return 0;
}
Output
The split string is:
i : 0 Program
i : 1 to
i : 2 split
i : 3 strings
i : 4 using
i : 5 custom
i : 6 split
i : 7 function.
The strtok() function
To split the original string, this function split it into tokens and pieces according to the delimiter you pass to the function. It is used to modify the original string along with putting a Null character on the delimiter.
Syntax
char *ptr = strtok( str, delim)
Check out the program code
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[100]; // declare the size of string
cout << " Enter a string: " <<endl;
cin.getline(str, 100); // use getline() function to read a string from input stream
char *ptr; // declare a ptr pointer
ptr = strtok(str, " , "); // use strtok() function to separate string using comma (,) delimiter.
cout << " \n Split string using strtok() function: " << endl;
// use while loop to check ptr is not null
while (ptr != NULL)
{
cout << ptr << endl; // print the string token
ptr = strtok (NULL, " , ");
}
return 0;
}
Output
Enter a string:
Learn how to split a string in C++ using the strtok() function.
Split string using strtok() function:
Learn
how
to
split
a
string
in
C++
Using
the
strtok()
function.
The find() and substr() function
This function is also a nice option to split strings. The find() function is used in the loop to look for delimiter occurrences in the specified string. Then, split it into tokens. Whereas the substr() function is used to print the stored sub-string. Have a look at the program
#include <iostream>
#include <string>
using namespace std;
int main()
{
// given string with delimiter
string given_str = "How_to_split_a_string_using_find()_and_substr()_function_in_C++";
string delim = "_"; // delimiter
cout << " Your string with delimiter is: " << given_str << endl;
size_t pos = 0;
string token1; // define a string variable
// use find() function to get the position of the delimiters
while (( pos = given_str.find (delim)) != std::string::npos)
{
token1 = given_str.substr(0, pos); // store the substring
cout << token1 << endl;
given_str.erase(0, pos + delim.length()); /* erase() function store the current positon and move to next token. */
}
cout << given_str << endl; // it print last token of the string.
}
Output
Your string with delimiter is: How_to_split_a_string_using_find()_and_substr()_function_in_C++
How
to
split
a
string
using
find()
and
substr()
function
in
C++
An erase() function is also used in the program code whose function is to store the string’s current position, and then make a move to the next token. This process continues till strings split.
The std::getline() function
It is a standard library function to read the string through input steam. Till you find the delimiter character, the function keeps it put into the vector string.
Syntax
getline(str, token, delim);
Take a look at the program
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
int main()
{
string S, T; // declare string variables
getline(cin, S); // use getline() function to read a line of string and store into S variable.
stringstream X(S); // X is an object of stringstream that references the S string
// use while loop to check the getline() function condition
while (getline(X, T, ' ')) {
/* X represents to read the string from stringstream, T use for store the token string and,
' ' whitespace represents to split the string where whitespace is found. */
cout << T << endl; // print split string
}
return 0;
}
Output
Welcome to the Tutopal and Learn C++ Programming Language.
Welcome
to
the
Tutopal
and
Learn
C++
Programming
Language.
Conclusion
And that’s it! You got all the solutions to split string by space in C++. I hope you find the methods and functions helpful! I wish you luck!
Reference Source: https://en.cppreference.com/w/cpp/string/basic_string/getline
https://appdividend.com/2020/02/17/cpp-strtok-function-example-strtok-in-cpp/