strings

Hello I have char[] with big text how i can extract every single word in different char[]?
I have tried that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void main()
{
	int n = 100;
	char* arr = new char[n];
	int m = 50;
	char* word = new char[m];
	cout<<"Input string: ";
	cin>> arr;
	n = strlen(arr);
	
	for(int i=0; i<n; i++)
	{
		if(arr[i] == ' ')
		{
			for(int j=0; j<i-1; j++)
			{
				word[i] = arr[i];
			        k = i;
			}
			for(int l=k+1; l<m; l++)
				word[l]= '\0';
		}
	}
	cout<< word;


but its don't work. Help me please.
Last edited on
make an array or vector of char *'s.
i want to make it for one word first but it doesn't work. Result of cout<< word is unreadable. why ???
change vword[i] = arr[i];[/code to [code]word[j] = arr[j];

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
char x[10] ="haha keke";
char z[10];
  for(size_t i = 0, j = 0; i < 10; ++i)
  {
    if(x[i] != ' ' && x[i] != '\0')
    {
      if(x[i] != ' ')
      {
        z[j] = x[i];
        ++j;
      }
      //cout<<"i = "<<i<<", "<<z[i]<<"\n";
    }
    else if(x[i] == '\0')
    {
      z[j] = '\0';
      //cout<<"i = "<<i<<", "<<z[i]<<"\n";
      break;
    }
  }
  cout<<z<<"\n"<<x<<"\n";


or

1
2
3
4
5
6
D = "haha keke";
std::string::iterator it = std::remove(D.begin(), D.end(), ' ');
D.erase(it);
//you could compose them by
//D.erase(std::remove(D.begin(), D.end(), ' '));
cout<<D<<"\n";
hi friend, im beginer too , i see "single word" in your question and not "single char" i think word contain some char, notify me if im wrong about it , so this is what can i do for your problem
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <cstdlib>
#include <iostream>

using namespace std;
class myword
{
	public:
		char text[20];
		int startpoint;
		int endpoint;
	myword()
		{
			endpoint=0;
			cout<<endl<<">>>>>>>constructor is runing<<<<<< ";
		}

	void getdata(char data[])
		{
			int i=0;
			cout<<endl;
			while	(
						(data[startpoint+i]!=' ')
			      	&&
					  	(strlen(data)>= (i+startpoint))
				  	)
					{
						text[i] = data[i+startpoint];
						i++;
					}
			text[i]=data[strlen(data)];
			endpoint = i+startpoint;
			cout<<endl<<"result : "<<text;
		};
};
int main()
{
	
	int n = 100;
	char* arr = new char[n];
	int m = 50;
	char* word = new char[m];
	cout<<"Input string: ";
	cin.getline(arr,m);
	n = strlen(arr);
	
	int whitespace =1;
	//count white space
	for(int i=0; i<= n;i++)
		{
			if(arr[i] == ' ')
			{
				whitespace++;
			}
			
		}
	cout<<endl<<"white space :  "<<whitespace<<endl;
	myword theword[whitespace];
	int begining = 0;
	for(int i=0;i<whitespace;i++)
		{
			theword[i].startpoint = begining;
			theword[i].getdata(arr);
			begining = theword[i].endpoint+1;
			
			cout<<endl<<"gained word : "<<i<<" : "<<theword[i].text;			
		}
	cout<<endl<<endl;
	system("pause");
	return EXIT_SUCCESS;
}


compiled well with cfree-5
Topic archived. No new replies allowed.