Storing words of a string into an array

I am having some trouble storing all the words of my string into an array. The last word of the sentence is not being stored into the array because there is no space at the end of the string.

I've tried placing if statements in several locations, but then it won't store any of the words into the array.

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
#include <iostream>
#include <string>
using namespace std;

struct Word {
 string english;
 string piglatin;
};

Word * splitSentence(const string words, int &size) {

	Word *wordArr = new Word[size];
	int position;
	int j = 0;
	cout << size << endl;

    for (unsigned int i = 0; i < words.length(); i++)
    {
        if (isspace(words[i])) {
            if (position != '\0') {
                position++;
            }
            wordArr[j].english = words.substr(position, i - position);
            cout << wordArr[j].english << " ";
            position = i;
            j++;
        }
    }
    return wordArr;
}
int main() {

	Word conversion;
	string sentence;
	int numWords = 0;

	cout << "Please enter a string to convert to PigLatin:" << endl;
	getline(cin, sentence);
	cout << endl;
	cout << sentence << endl;

	for(int i = 0; sentence[i] != '\0'; i++) {
		if (sentence[i] == ' ') {
			numWords++;
		}
	}

	numWords = numWords +1;
	splitSentence(sentence, numWords);

	return 0;
}
Last edited on
Hello, I am not sure on this, but I see the keyword "new" being used without the keyword "delete" anywhere in your code. Now does this not create a memory leak?
I just added the delete. But I'm more focused on figuring out how to make sure that the last word of my string is included into my array.
It was hard for me to follow your code, but I get what you are trying to do.
I recommend using a vector to store the words of your string. Play around with this.

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
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <string>
#include<vector>
#include<sstream>

using namespace std;

struct Word {
	string english;
	string piglatin;
};

Word * splitSentence(const string words, int &size) {

	Word *wordArr = new Word[size];
	int position = 0;
	int j = 0;
	cout << size << endl;

	for (unsigned int i = 0; i < words.length(); i++)
	{
		if (isspace(words[i])) {
			if (position != '\0') {
				position++;
			}
			wordArr[j].english = words.substr(position, i - position);
			cout << wordArr[j].english << " ";
			position = i;
			j++;
		}
	}
	return wordArr;
}

int main() {

	Word conversion;
	string sentence;
	int numWords = 0;

	cout << "Please enter a string to convert to PigLatin:" << endl;
	getline(cin, sentence);
	cout << endl;
	cout << sentence << endl;

	for (int i = 0; sentence[i] != '\0'; i++) {
		if (sentence[i] == ' ') {
			numWords++;
		}
	}

	numWords = numWords + 1;
	cout << numWords << endl;
	

	//splitSentence(sentence, numWords);


	//to split the string
	istringstream iss(sentence);

	vector<string> myVector;
	int i = 0;
	do {
		string substring;
		iss >> substring;
		myVector.push_back(substring);
		cout << "Substring: " << myVector[i] << endl;
		i++;
	} while (iss);

	




	cin.ignore();
	cin.get();
	return 0;
}
Topic archived. No new replies allowed.