Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separating a string in C++

I am trying to separate a string into multiple strings, to make a customized terminal. So far I have been separating control signals using strtok, however I do not understand how to separate specific instances of a character. For example:

string input = "false || echo \"hello world\" | grep hello";

When trying to strtok this input and trying to separate using | the output would be:

false , echo "hello world" , grep hello

Instead, I would like the output to be:

false || echo "hello world" , grep hello

How can I have strtok treat | and || differently rather than having it saying they are the same?

like image 445
divyanshch Avatar asked May 10 '15 07:05

divyanshch


People also ask

How do I split a string into another string?

Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.

How do you separate data from a string?

Use the string split in Java method against the string that needs to be divided and provide the separator as an argument. In this Java split string by delimiter case, the separator is a comma (,) and the result of the Java split string by comma operation will give you an array split.


2 Answers

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

vector<string> split(string sentence,char delim)
{
    string tempSentence = "";
    tempSentence += delim;
    tempSentence += sentence;
    tempSentence += delim;

     string token;
     vector<string> tokens;
    for (int i=1;i<tempSentence.length()-1;++i)
    {
        if (tempSentence[i] == delim && tempSentence[i-1] != delim && tempSentence[i+1] != delim)
        {
            if (token.length()) tokens.push_back(token);
            token.clear();
        }
        else
        {
            token += tempSentence[i];
        }
    }
    if (token.length()) tokens.push_back(token);

    return tokens;
}

int main() {
    string sentence = "false || echo \"hello world\" | grep hello";
    char delim='|';

    vector<string> tokens = split(sentence,delim);


    for_each(tokens.begin(), tokens.end(), [&](string t) {   
        cout << t << endl;
    });

}

ugly and long! but works!

like image 102
hich9n Avatar answered Oct 09 '22 10:10

hich9n


strtok() is going to scan character by character, without regard to characters before and after what it is looking for. If you want a smarter scan, you'll need to implement the additional check yourself.

Since strtok just returns a location within the string where a token is found, you'd have to manually check the first character of the token being returned to see if it is also a '|', and then act accordingly.

A better solution would be to look into the use of a regular expression here. It sounds like the symbol you want to split on is not just a |, but rather a | surrounded by spaces -- ie, you are actually searching and splitting on a three character symbol (space - pipe - space)

like image 39
Barry Gackle Avatar answered Oct 09 '22 10:10

Barry Gackle