elimination!

hello, I want to create app that will eliminate some letter if condition is met. And I have this code which eliminates 'D' or 'd' characters, but at output i just get the same word. (i.e. if I enter word "dream", at output I get the same, and I expect to get "ream")
here is code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#define LENGTH 12

using namespace std;

int main() {
char _word[LENGTH];
    cout<<"enter word: ";
    cin>>_word;
    cout<<"\n"<<_word;      //displays how the entered word looks
                            //before elimination
for(int n=0;n<LENGTH;n++)     //repeat down actions until the end of word
    {                           // is met
        if(_word[n]=='d' || _word[n]=='d') { //elimination
            _word[n]='\0';                  //substitute D or d with \0
                    }
    }
    cout<<"\n"<<_word;   //print modified word
return 0;
}

Last edited on
This way won't work. When you put a '\0' character in a c-string, this automatically indicates that your string ends there. Nothing after that point will be printed. I'd suggest creating another char array and copying the initial array to the new one, omitting the characters you don't want.
here is new code with new array as m4aster suggested.
But (there is always but) I am getting unexpected results, and I can't see what is wrong here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#define LENGTH 12

using namespace std;

int main() {
char _word[LENGTH];
char modified_word[LENGTH];
    cout<<"enter word: ";
    cin>>_word;
    cout<<"\nBefore: "<<_word;

for(int n=0;n<LENGTH;n++)
    {
            if(_word[n]=='d' || _word[n]=='d') {
            continue;
                    }
            else {
            modified_word[n]=_word[n];
                    }
    }
    cout<<"\n"<<modified_word;
return 0;
}

Last edited on
Why not use std::string? Also, the STL algorithm remove/remove_if are perfect for this.
Line 19 is wrong because you dont have the all 'd' s written but the length of the modified_word decreases then corrected one must be like 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
#include <iostream>
#define LENGTH 12

using namespace std;

int main() {
char _word[LENGTH];
int counter = 0;
char modified_word[LENGTH];
    cout<<"enter word: ";
    cin>>_word;
    cout<<"\nBefore: "<<_word;

for(int n=0;n<LENGTH;n++)
    {
            if(_word[n]=='d' || _word[n]=='D') {
            counter++; continue;
                    }
            else {
            modified_word[n-counter]=_word[n];
                    }
    }
    cout<<"\n"<<modified_word;
cin.ignore(2);
return 0;
}

Last edited on
@systempause: i still dont get it, can you pls explain deeper?
@moorecm: remove_if algorith, how could I use it on my example? I found template and example on this site but its still complicated for me. I learn C++ for 4 days
I think the problem is that you need to monitor the position in the modified word independently of the position in the word you are skipping.

Otherwise you end up with gaps in the modified word in the same position that you jumped over the letter from the input word:

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
#include <iostream>
#define LENGTH 12

using namespace std;

int main()
{
	char _word[LENGTH];
	char modified_word[LENGTH];
	cout << "enter word: ";
	cin >> _word;
	cout << "\nBefore: " << _word;

	int m = 0; // keep track of modified word's next letter
	for(int n = 0; n < LENGTH; n++)
	{
		if(_word[n] == 'd' || _word[n] == 'D')
		{
			continue;
		}
		else
		{
			modified_word[m] = _word[n];
			m++; // increment position every time a new letter added
		}
	}
	cout << "\n" << modified_word;
	return 0;
}
Last edited on
finally, here is moorecm 's suggestion, looks pretty nice:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <algorithm>
#define LENGTH 12
using namespace std;

bool checkit (char i) {
   if (i=='D' || i=='d') return true; 
   else return false;
  }
int main() {
char _word[LENGTH];
     cout<<"Enter word(max. 12 letters): ";
     cin>>_word;
//bounds
char* begin=_word;
char* end=_word+sizeof(_word)/sizeof(char);
          end = remove_if(begin, end, checkit);
          cout<<"\nModified word :"<<_word;
return 0;
}

If this is a college assignment then they probably want to see you iterate through the loop yourself. Otherwise moorecm is right and using the STL algorithms is a great solution.

You might also want to consider using std::string rather than a character buffer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
	std::cout << "enter a word: ";
	std::string word;
	std::cin >> word;
	word.assign(word.begin(), std::remove(word.begin(), word.end(), 'd'));
	word.assign(word.begin(), std::remove(word.begin(), word.end(), 'D'));

	std::cout << "word = " << word << std::endl;

	return 0;
}
excellent solution too.
btw this wasnt college assignment. I was going through arrays in tutorial on this site, and while reading i got idea to do this task ^^
Topic archived. No new replies allowed.