-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit string and put it into an arrray.cpp
More file actions
85 lines (72 loc) · 2.28 KB
/
split string and put it into an arrray.cpp
File metadata and controls
85 lines (72 loc) · 2.28 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// 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>
#include <vector>
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;
}
}
}
// Function to split a string into a vector of substrings (beginner level)
vector<string> string_to_array(const string& s) {
vector<string> result;
if (s.empty()) {
return result; // return empty vector if input is empty
}
string word = "";
for (int i = 0; i < s.length(); i++) {
if (s[i] == ' ') {
if (word != "") {
result.push_back(word);
word = "";
}
} else {
word += s[i];
}
}
if (word != "") {
result.push_back(word);
}
return result;
}
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;
}