#include <iostream>
#include <vector>
#include <sstream>
// Function to split a string into a vector of substrings based on a delimiter
std::vector<std::string> splitString(const std::string& input, char delimiter) {
std::vector<std::string> tokens; // Create a vector to store the substrings
std::stringstream ss(input); // Create a stringstream to process the input string
std::string token; // Temporary variable to store each substring
// Loop through the stringstream until there are no more tokens
while (std::getline(ss, token, delimiter)) {
tokens.push_back(token); // Add each substring to the vector
}
return tokens; // Return the vector of substrings
}
int main() {
std::string inputString = "Hello,World,C++,Split,Example";
char delimiter = ','; // Define the delimiter character
// Call the splitString function to get the vector of substrings
std::vector<std::string> substrings = splitString(inputString, delimiter);
// Display the original string
std::cout << "Original String: " << inputString << std::endl;
// Display each substring in the vector
std::cout << "Substrings:" << std::endl;
for (const std::string& substring : substrings) {
std::cout << substring << std::endl;
}
return 0; // Return 0 to indicate successful execution
}