seperating the memebers in the string

Hi ,
I am trying to seperate the word in the string .
but not getting the proper result .
This is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
int main(int argc, char *argv[])
{
	string mid_list ; "111	  222   		333	   444	   555	   666";
	string * array ; 
	string word ;
	char prev_ch ; 
	int word_count  = 0 , count = 0;  
	char ch ; 
	for( int i = 0 ; i <  mid_list.length()  ; i++ ) 
	{
		ch = mid_list[i] ; 
		if( ch !=- " " ) 
		{
			word += ch ; 
			prev_ch = ch ; 
		}
		else  if ( prev_ch != " " )
		{
			prev_ch = " ";
			array[count] = word ; 
			count++; 
			word.empty(); 
		}
	}
	
for( int i = 0 ; i < count ; i+) 
    cout<<"The array is = "<<array[i];

	
	return 0;
}

Thanks in advance .

Last edited on
First fix the compile errors. Start with the first error and work your way through.
Last edited on
I tried doing something similar recently. Does the find function apply anywhere in this? Your question isn't quite lucid enough.
just string stream it into a char array and tell it to seperate at blank spaces
I suppose the "proper result" that you wish to get is an array (or a linear structure) of strings that contains the individual elements in the input separated by white spaces?

This process is referred to string tokenization. In languages in Java, there are built-in tokenizers to do the job. In C++, there is also a cleaner way to do this.
(There are many subtleties when implementing tokenization -- essentially one has to exhaust the situations in which something goes wrong in the input.)

We would use the libraries String, stringstream, STL algorithms along with iterators, and suppose we use vector as the container:
1
2
3
4
5
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <vector> 


1
2
3
4
5
6
7
8
9
using namespace std;

string input("111	  222   		333	   444	   555	   666");
istringstream isstr(input);
vector<string> tokens;

copy( istream_iterator<string>(isstr),
      istream_iterator<string>(),
      back_inserter< vector<string> >(tokens) );


The back_inserter is to perform overwriting in preference to insertion at the end of the vector.

If you are just printing out the tokens without storing them, the third argument of copy() can be replaced by the iterator directly to the output device (std::cout):
ostream_iterator<string>(cout, "\n").

A similar method applies if you want to convert the tokens further into ints, or other types.
Last edited on
the part that concerns me is
1
2
3
4
5
6
7
else  if ( prev_ch != " " )
		{
			prev_ch = " ";
			array[count] = word ; 
			count++; 
			word.empty(); 
		}



This gives me run time error . i know i have to allocate memory for the array[count] . but how do i allocate memory and also delete memory , if i dont know the number of digets in mid_list in advance
any one on this .
Topic archived. No new replies allowed.