-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.cpp
More file actions
61 lines (49 loc) · 1.67 KB
/
split.cpp
File metadata and controls
61 lines (49 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// to split an string we have to use library function called istringstream
/*approach:
1 Create an input string stream from the input string using std::istringstream.
2 Iterate through the stream, using std::getline to extract each substring separated by the delimiter.
3 Add the extracted substring to the array.
4 Print the array of substrings.*/
// C++ Program to illustrate how to split a string into an
// array of substrings
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
// Function to split a string into tokens based on a
// delimiter
void splitString(const string& input, char delimiter,
string arr[], int& index)
{
// Creating an input string stream from the input string
istringstream stream(input);
// Temporary string to store each token
string token;
// Read tokens from the string stream separated by the
// delimiter
while (getline(stream, token, delimiter)) {
if (!token.empty()) {
// Add the token to the array
arr[index++] = token;
}
}
}
int main()
{
// Input string
string input = "Hello, I am Geek from Geeksforgeeks";
// Delimiter
char delimiter = ' ';
// Array to store the substrings
string arrayOfSubStr[100];
// Index to keep track of the number of substrings
int index = 0;
// Calling the function to split the input string into
// an array of substrings
splitString(input, delimiter, arrayOfSubStr, index);
// Print the array of substrings
for (int i = 0; i < index; i++) {
cout << arrayOfSubStr[i] << endl;
}
return 0;
}