0

how can I split (with ,) a string and match every element? I'm using gcc version 4.6.3 on Ubuntu.

My "pseudo-code":

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

int main () {
    char str[] = "foo,bar";
    char * pch;
    pch = strtok (str,",");

    for (int i = 0; i < str.size(); ++i) {
       if( pch[i] == "something" ) {
          cout << pch[i] << "\r\n";
       }
   }
}

Thank you

Alfons
  • 311
  • 1
  • 8
  • 17
  • @Mgetz that question asks about splitting strings by whitespace. – Tim Seguine Nov 12 '14 at 20:59
  • @TimSeguine scroll down to the second answer – Mgetz Nov 12 '14 at 21:00
  • @Mgetz I know, I read it. But as I understand it a question is a duplicate if the *question* is duplicated, not if the answers are applicable. – Tim Seguine Nov 12 '14 at 21:02
  • Why does your question title have the words "foreach array"? I don't understand how that is relevant to the question you asked in the post. – Tim Seguine Nov 12 '14 at 21:06
  • @TimSeguine the question is how to split a string in C++, the delimiter should be irrelevant ultimately. – Mgetz Nov 12 '14 at 21:08
  • @Mgetz but if you mark it a duplicate as is, it would most likely only confuse people, since the accepted answer only splits on whitespace, which is precisely what was asked for in that question. – Tim Seguine Nov 12 '14 at 21:10
  • @TimSeguine http://meta.stackexchange.com/a/231212/226653 – Mgetz Nov 12 '14 at 21:12
  • @Mgetz I am familiar with that post, but in my opinion that doesn't apply since the **accepted answer** to that question won't solve this OP's problem. It's more like "many questions have similar or identical answers but are not duplicates". I agree that the proper solution to both problems does not depend on the delimiter. I don't feel like arguing about this further, I am only explaining why I don't think this should be flagged. I doesn't bother me if you wanted to. – Tim Seguine Nov 12 '14 at 21:20
  • @user476918 I gave you a ready to use solution. Do not take into account that some idiots downvoted my answer.:) – Vlad from Moscow Nov 12 '14 at 21:30

1 Answers1

-3

For example

#include <iostream>
#include <cstring>

int main() 
{
    char str[] = "foo,something,bar";

    char *p = std::strtok( str, "," );
    while ( p )
    {
        if ( std::strcmp( p, "something" ) == 0 ) std::cout << p << std::endl;
        p = std::strtok( NULL, "," );
    }

    return 0;
}

The output is :)

something

An other approach is to use class std::string and std::istringstream. For example

#include <iostream>
#include <sstream>

int main() 
{
    char str[] = "foo,something,bar";
    std::istringstream is( str );
    std::string word;

    while ( std::getline( is, word, ',' ) )
    {
        if ( word == "something" ) std::cout << word << std::endl;
    }

    return 0;
}

The output will be the same as above.

If you need to store separate words then you can use class std::vector<std::string> to store them.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335