Complete stumped on how to remove certain parts of a string

My assignment is to translate one language into another, and a subset of this assignment is removing parts of an entered string, specifically removing "the" and "a".

So for example:
User types in : "the dog ate the cat"
output is: "dog ate cat"

User types in : "a dog ate a cat"
output is: "dog ate cat"

User types in : "the fire burned a person"
output is: "fire burned person"

What I have so far is just replacing "the" and "a" in the string with ' ' (empty spaces) and having compiler reread from the beginning of the string so "the man" becomes " man", man is stored in string "w1" and w1 is outputted. This code always crashes.

Is there an easier way to do 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
 #include <iostream>

using namespace std;

int main()
{
string S;
getline (cin, S);

int a = 0;
while (S[a] == 't')
{
    S[a] = ' ';
    a++;
    S[a] = ' ';
    a++;
    S[a] = ' ';
    a++;
}

while (S[a] == 'a')
{
    S[a] = ' ';
    a++;
}

string w1 = "";
int b = 0;
while (S[b] = ' ')
    b++;
while (S[b] != ' ')
    w1 = w1 + S[b++];

cout << S << S.length() << w1 << endl;

    return 0;
}


note: I'm not allowed to use functions, void data types, as they have not been gone over in class yet.
Last edited on
This might be of interest to you.

http://www.cplusplus.com/reference/string/string/find/

Also note the related functions at the bottom of the linked reference.
Topic archived. No new replies allowed.