split string on character vector C++

To split a string on a character in C++, you can use the std::getline function from the <string> library. Here are the steps to do it:

  1. Include the necessary header files:
#include <iostream>
#include <string>
#include <vector>
  1. Declare a string variable to store the input string:
std::string inputString;
  1. Take user input for the string:
std::cout << "Enter a string: ";
std::getline(std::cin, inputString);
  1. Declare a character variable to store the splitting character:
char splitChar;
  1. Take user input for the splitting character:
std::cout << "Enter the splitting character: ";
std::cin >> splitChar;
  1. Declare a vector of strings to store the splitted substrings:
std::vector<std::string> substrings;
  1. Use a std::stringstream object to split the string:
std::stringstream ss(inputString);
std::string substring;
  1. Iterate over the string using std::getline and split it on the specified character:
while (std::getline(ss, substring, splitChar)) {
    substrings.push_back(substring);
}
  1. Print the splitted substrings:
std::cout << "Splitted substrings:" << std::endl;
for (const auto& str : substrings) {
    std::cout << str << std::endl;
}

That's it! The above steps demonstrate how to split a string on a character in C++. Let me know if you need further assistance!