Home > front end >  How to split a string and record each split as a distinct variable C
How to split a string and record each split as a distinct variable C

Time:03-22

I was looking at a popular StackOverflow post about how to split strings. I have found this very useful, but I'd like to take each split and store it in array or a distinct string variable. Such that I can access: scott, tiger, mushroom, or fail. Below is my attempt to do this, but I cannot complile this due to an error:

cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'char' in assignment

Does anyone know an efficient way of doing this?

#include <bits/stdc  .h>
using namespace std;
     
#include <stdio.h>
#include <string.h>
     
char str[10];
void setup(){
    std::string s = "scott>=tiger>=mushroom>=fail";
    std::string delimiter = ">=";
    int arrayIndex = 0;
    
    size_t pos = 0;
    std::string token;
    while ((pos = s.find(delimiter)) != std::string::npos) {
        token = s.substr(0, pos);
        std::cout << token << std::endl;
        s.erase(0, pos   delimiter.length());
        str[arrayIndex  ] = token;
        
    }
    std::cout << s << std::endl;
}
    
void loop(){
}

CodePudding user response:

str[] is an array of individual chars, but you are trying to store std::string objects in it, hence the error. Change the array to hold std::string instead of char. And then consider using std::vector instead of a fixed array.

#include <iostream>
#include <string>
#include <vector>
     
std::vector<std::string> str;

void setup(){
    std::string s = "scott>=tiger>=mushroom>=fail";
    std::string delimiter = ">=";
    
    size_t start = 0, pos;
    std::string token;

    while ((pos = s.find(delimiter, start)) != std::string::npos) {
        token = s.substr(start, pos-start);
        str.push_back(token);
        start = pos   delimiter.size();
    }
    if (start < s.size())
        str.push_back(s.substr(start));

    for(auto &elem : str)
        std::cout << elem << std::endl;
}

Online Demo

CodePudding user response:

TRY this:

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE Suites
#include <boost/test/unit_test.hpp>
#include <stdio.h>
#include <string>
#include <unordered_map>

class StringLabelAdapter
{
std::unordered_map<std::string, int> firstLabelFound;
public:

StringLabelAdapter(std::string& stringPassedIn, std::string& delimiter):firstLabelFound()
{
    int arrayIndex = 0;

    size_t delimiterPosition = 0, prevDelimiterPosition = 0;

    while ((delimiterPosition = stringPassedIn.find( delimiter, prevDelimiterPosition)) != std::string::npos)
    {
        std::string token = stringPassedIn.substr(prevDelimiterPosition, delimiterPosition);
        if (!Exists(token))
        {
            firstLabelFound[token] = prevDelimiterPosition   1;
        }
        prevDelimiterPosition  = token.size()   delimiter.size();
    }
    };

    bool Exists(std::string& labelToCompare) { return firstLabelFound.find(labelToCompare) != firstLabelFound.end(); };
    };
    BOOST_AUTO_TEST_SUITE(TestStringLabelAdapterSuite)
    BOOST_AUTO_TEST_CASE(TestStringLabelAdapter)
    {
    std::string stringPassedIn = "scott>=tiger>=mushroom>=fail";
    std::string delimiter = ">=";
    StringLabelAdapter test(stringPassedIn,delimiter);
    std::string s("scott");
    BOOST_CHECK(test.Exists(s));
    }
    BOOST_AUTO_TEST_SUITE_END()

if you don't want boost unit test remove the boost header and the boost suite and put it in main.

  • Related