array, sentence, word elimination

write a code to eliminate a word in a given sentence! compiles fine. doesn't work!!! can anybody tell me why???

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
  #include <iostream>
#include <string>
using namespace std;
char space = ' ';
int counting = 0, counting_2 = 0;
bool word_found = false;
int c, note;


void eliminate_word(char sentence[], char word[]);

int main (){
char phrase[1000], parola[50];
cout<<"Enter a phrase or sentence..."<<endl;
cin.getline(phrase, 999);

cout<<endl<<endl<<"Enter word to be eliminated..."<<endl;
cin>>parola;



eliminate_word(phrase, parola);

}


void eliminate_word(char sentence[], char word[]){

for (int i=0; i<50 ;i++ ){ if(word [i] != '\0' || word [i] != space)
                              { counting ++;}
}

//searching the beginning of a word and checking lenght for elimination
for(int a = 0; a < 1000; a++) { 
if (sentence [a]==word[0] && sentence[a-1] == space)
                                  { note = a; c = (note + counting-1) /* to help check if at least lenght is equal*/ ;}


for (int k = a; k < c; c++) {if (sentence[k] != 0 || sentence [k]!= space || sentence [k] != '\0')  counting_2 ++; }

if (counting_2 == counting){ for(int i = 0; i<counting; i++){ if (word[i]== sentence [note+i]) word_found = true;}


if (word_found == true)
for (int i = 0; i< counting; i++){sentence[note+i] = space;}
                                                                     } 
                                                                     }
 
 cout<<"Sentence with word eliminated is "<<endl<<sentence<<endl;                                                                    
                                                                     
                                                                 }
Use std::string to hold the sentence and the word to be eliminated,
std::string::find() to locate the white-space-delimited word,
std::string::erase() to erase the word and the white space following it.

http://www.cplusplus.com/reference/string/string/find/
http://www.cplusplus.com/reference/string/string/erase/
Topic archived. No new replies allowed.