. Advertisement .
..3..
. Advertisement .
..4..
This article will look at how to split string by space in C++ programming language. The division or split of the string occurs when a set of words or string collections are broken down into single words. Splitting strings, on the other hand, requires the use of delimiters such as white space (), comma (,), hyphen (-), and so on, resulting in separate words.
Different Ways To Split String By Space In C++
There is no built-in split method for separating a group of strings into separate strings. So, in this section, we will go through the various techniques for splitting strings into a single one in C++.
- To divide strings, use the strtok() function.
- To separate strings, use the custom split() method.
- To split a string, use the std::getline() method.
- To divide a string, use the find() and substr() functions.
Split Strings With The strtok() Function
strtok(): The delimiter given to the strtok() method is used to break the original text into parts or tokens.
Syntax char *ptr = strtok( str, delim) A strtok() function takes two parameters in the above syntax: str and delim. str: The strtok() method splits strings from an original string. delim: It is a character that’s used to break up a string. Commas (,), spaces (), hyphens (-), and other symbols are examples. Return: Giving you a pointer to the following character tokens. Initially, it refers to the first string character. |
The original string is modified on each call to the strtok() method by inserting a NULL character (‘0’) at the delimiter point. It will be easier to keep track of the token’s status this way. Consider the following example of splitting a string in C++ using the strtok() function.
Program.cpp
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
// declare the size of string
char str[100];
cout << ” \n Enter your string: “ <<endl
// use getline() function to read a string from input stream
cin.getline(str, 100);
// declare a ptr pointer
char *ptr;
// use strtok() function to separate string using comma (,) delimiter
ptr = strtok(str, ” , “);
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 your string:
How to Split String by space in C++
Split string using strtok() function:
How
to
Split
String
by
space
in
C++
Using A Custom split() Method
Let’s create a C++ application that uses a custom split() method to divide string sequences.
Program2.cpp
#include <iostream>
#include <string>
// Define the max string
#define max 10
using namespace std;
// Define max string
string strings[max];
// Length of the string
int len(string str)
{
int length_str = 0;
for (int i = 0; str[i] != ‘\0’; i++)
{
length_str++;
}
return length_str;
}
// Create custom split() function
void split (string str, char separator)
{
int current_Index = 0, i = 0;
int start_Index = 0, endIndex = 0;
while (i <= len(str))
{
if (str[i] == separator || i == len(str))
{
endIndex = i;
string subStr = “”;
subStr.append(str, start_Index, endIndex – start_Index);
strings[current_Index] = subStr;
current_Index += 1;
start_Index = endIndex + 1;
}
i++;
}
}
int main()
{
string str = “The solution to split strings by space in C++ using a custom split function”;
char separator = ‘ ‘; // Space
split(str, separator);
cout <<” The splited string is: “;
for (int i = 0; i < max; i++)
{
cout << “\n i : “ << i << ” “ << strings[i];
}
return 0;
}
Output
The splited string is:
i : 0 The
i : 1 solution
i : 2 to
i : 3 split
i : 4 strings
i : 5 by
i : 6 space
i : 7 in
i : 8 C++
i : 9 using
i : 10 a
i : 11 custom
i : 12 split
i : 13 function
Split A String Using The std::getline() Function
A getline() method in the C++ standard library reads a string from an input stream and stores it until a delimiter character is found in a vector string. Importing the string> header file allows us to utilize the std::getline() method.
Syntax getline(str, token, delim); It has three settings: str: A variable that holds the original string. token: It holds the extracted string tokens from the original string. delim: A character used to split a string. Commas (,), spaces (), hyphens (-), and other symbols are examples of punctuation. |
Consider the following scenario for splitting strings with the C++ getline() method.
Program3.cpp
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
int main()
{
// Declare string variables
string S, T;
// Use getline() function to read a line of string and store into S variable.
getline(cin, S);
// X is an object of stringstream that references the S string
stringstream X(S);
// 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. */
// Print split string
cout << T << endl;
}
return 0;
}
Output
Welcome to ITtutoria and Learn C++ Programming Language.
Welcome
to
ITtutoria
and
Learn
C++
Programming
Language.
Use find() and substr() To Separate Strings
Let’s develop a C++ program that splits provided strings using the find() and substr() functions.
Program4.cpp
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
void split_str( std::string const &str, const char delimiter,
std::vector <std::string> &out )
{
// Create a stream from the string
std::stringstream s(str);
std::string s2;
while (std:: getline (s, s2, delimiter) )
{
// Store the string in s2
out.push_back(s2);
}
}
int main()
{
std:: string s2 = “The solution of split string by space in C++”;
const char delimiter = ‘ ‘; /* Define the delimiteriter like space (‘ ‘), comma (,), hyphen (-), etc. */
std::cout << “Your string is: “ << s2;
// Store the string in vector
std::vector <std::string> out;
// Call function to split the string
split_str (s2, delimiter, out);
// Use range based for loop
for (const auto &s2: out)
{
std::cout << “\n” << s2;
}
return 0;
}
Output
Your string is:
The solution of split string by space in C++
The
solution
of
split
string
by
space
in
C++
Conclusion
This article explains four distinct ways to split string by space in C++ using basic examples to assist new users in performing the split operation in C++. You can go through the information in this post again to make sure you remember it.
Leave a comment