0

The input is a standard string input. Like this.

"Anand,Ramesh,Suresh#Anand,Ramesh,Suresh,Suresh,Anand"

I want to get get all the Names before the # and store it in an array. And use the names after the # to do some operation.

How do I copy the first three names before # to an array. So far I've figured out how to copy string seperated by comma. I couldn't find a solution to stop after a particular element is found.How do I stop when I encounter a #. Here is my code so far:

void findCombination(string input)
{
   stringstream ss(input); 
   string buffer; 
   vector<string>names;
   int i=0; 

   while(getline(ss,buffer,','))
   {
         names.push_back(buffer); 
   }


   for(int i=0;i<names.size();i++)
           cout << names[i] <<endl; 

   //return NULL; 

}
Shivji
  • 39
  • 1
  • 7
  • 1.do splitting on `#`. 2. then again do splitting according to `,` on 0th index. – Avinash Raj Aug 15 '15 at 13:12
  • @AvinashRaj I used a vector to take the first substring before the #. And tried to assign the value to ss(sstream) using ss << substream[0]. but this didn't work for some reason. 0th location has the string before the # – Shivji Aug 15 '15 at 13:34
  • try a first `getline()` with `#` as separator – Christophe Aug 15 '15 at 13:36

1 Answers1

0

There are plenty of ways doing it. THe simplest would be:

void findCombination(string input)
{
   string filteredinput(input,0, input.find('#'));  
   stringstream ss(filteredinput);  
   ... // rest of your code unchanged
}
Christophe
  • 68,716
  • 7
  • 72
  • 138