0

The function I am using is

vector<string> tokenise(string s){

}

Firstly, I intend to split the string into substrings, in which case the string is always arithmetic expression (e.g. "100+5") and there could be some whitespaces.

"100+5" is needed to convert to "100", "+", "5"

After conversion, the substrings will be stored in a vector and return it. I am struggling with the fist step and using the subscript to loop over a string. The type of the value returned is char, so there is no way to put it in the vector.

7
  • 2
    You can convert a char to a string using std::string str(1, ch); Commented May 3, 2017 at 4:49
  • 3
    If you only use strings with one character, you may as well use char Commented May 3, 2017 at 4:51
  • @InternetAussie As I asked, I have to use strings, which leaves me no options. Commented May 3, 2017 at 4:57
  • @AjayBrahmakshatriya Can you explain further in actual code? Commented May 3, 2017 at 4:59
  • If that's the case, why not return std::vector<char>? Commented May 3, 2017 at 5:25

3 Answers 3

2

You could just call the string's substring method, after figuring out the range of characters that are either digits, arithmetic characters, or unwanted.

Sign up to request clarification or add additional context in comments.

1 Comment

It doesn't work for me, coz the range of string to distinguish digits and operators is uncertain.
1

You mentioned - The type of the value returned is char, so there is no way to put it in the vector.

You have some function that returns a character. You want to then insert the equivalent string into the vector.

Assuming your vector is defined as

std::vector<std::string> broken_strings;

So you can do it as follows.

char ch = ...; // Here comes the character that you get from the function.
std::string str(1, ch);
broken_strings.push_back(str);

Then you can return broken_strings.

Edit:

OP mentions that he wants to tokenize algebraic expressions. So it will have to be done it a different way.

Following is a simple approach.

 std::vector<std::string> broken;
 std::string temp;
 for ( int i = 0; i<s.length() ;i++){
     char ch = s[i];
     if (ch == ' ')
         continue;
     else if (ch >= '0' && ch <='9')
         temp += ch;
     else{
         if (temp.length() != 0)
             broken.push_back(temp);
         temp = "";
         temp += ch;
         broken.push_back(temp);
         temp = "";
     }
 } 
 if (temp.length() != 0)
     broken.push_back(token);
 return broken; 

You can see the demo of the same here

Ideone

7 Comments

That, or with string::substr
You have completely changed your question. Now you need some logic to identify where a token ends. You might want to look at lexing tools like lex or flex.
sorry for that, is there any easy method to solve this.
Assuming you have set of characters that can be the operators, it can be done. I will edit my answer to do the same.
Where do you declare the "s"
|
0
vector<string> tokenise(string s)
{
    vector<string> v;

    string number;

    for(int i = 0; i < s.length(); ++i)
    {
        if((s[i] >= '0') && (s[i] <= '9'))
        {
            number += string(1, s[i]);
        }
        else if(s[i] == '.')
        {
            number += string(1, s[i]);
        }
        else if((s[i] == '+') || (s[i] == '-') || (s[i] == '*') || (s[i] == '/'))
        {
            if(number.size())
            {
                v.push_back(number);
                number.clear();
            }
            v.push_back(string(1, c));
        }
    }

    if(number.size())
    {
        v.push_back(number);
        number.clear();
    }

    return v;
}

1 Comment

I like your solution, almost the same as my idea

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.