Hello All:

Can anyone give me a simple idea how to remove the duplicated letters in a text
if I have this word HELLO it must be HELO.....

Thanks in advance.....

Recommended Answers

All 14 Replies

I think you might be able to get it done if you use cin.peek() and cin.ignore(). You'll have to loop them so that if the next character matches the current, the next is ignored.

Hello BJB:
Thanks for the answer but can you explain it with a simple code???
Thanks

>Can anyone give me a simple idea how to remove the duplicated letters in a text
Can you be more specific? Should the two L's be removed in "SLOWLY"? If so, which L should be removed? Are the characters to be ordered in any special way? All of these play a part in figuring out your solution.

Hello :
The last L should removed so the word must be SLOWY...
Thanks

Store the characters you've found in a suitable data structure and check the data structure with each new character. If it was seen before, don't do anything, otherwise add it to the data structure and print it. Here's an example using the standard set class:

#include <iostream>
#include <set>

int main()
{
  std::set<char> used;
  char ch;

  while ( std::cin.get ( ch ) ) {
    if ( used.find ( ch ) == used.end() ) {
      used.insert ( ch );
      std::cout.put ( ch );
    }
  }
}
#include <iostream>
#include <string>
using namespace std;

int main() {
	string text = "Hello World!!!";
	string tmp = "";
	for(size_t i = 0; i < text.size(); ++i){
		if (text[i] != text[i+1]){
			tmp+= text[i];
		}
	}
	cout << tmp;
	return 0;
}

Hello Narue:
Thanks a lot for your answer , what is
#include<set.h>??

ivailosp, that only removes consecutive duplicates. You failed to handle the specific case that I asked about, where "slowly" becomes "slowy".

>what is #include<set.h>??
It's a standard header for the set class (but note that it's <set>, not <set.h>). The set class is basically a data structure that allows you to store and quickly search for items.

not very good code, but works :P

#include <iostream>
#include <string>
using namespace std;

int main() {
	string text = "slowly";
	string tmp = "";
	for(size_t i = 0; i < text.size(); ++i){
		int poss = text.find_last_of(text[i], i);
		if (poss == 0){
			tmp+= text[i];
			continue;
		}
		if ((text.find_last_of(text[i], poss-1) == EOF) ){
			tmp+= text[i];
		}
	}
	cout << tmp;
	return 0;
}

Interesting solution. I have one nit though (actually I have several, but there's only one I would call an error):

>if ((text.find_last_of(text, poss-1) == EOF) ){
find_last_of returns string::npos if it doesn't find a match. EOF isn't required to have the same value as string::npos.

ok, ok :P

#include <iostream>
#include <string>
using namespace std;

int main() {
	string text = "teeeesting";
	string tmp = "";
	tmp+= text[0];
	for (size_t i = 1; i < text.size(); ++i) {
		if ((text.find_last_of(text[i], text.find_last_of(text[i], i)-1) == string::npos)) {
			tmp+= text[i];
		}
	}
	cout << tmp;
	return 0;
}

happy now

>happy now
I'm always happy. It comes from confidence in being right and having a thick skin. ;) I am curious though, why did you choose that particular solution over something like this:

#include <iostream>
#include <string>

int main()
{
  std::string text = "hello world";
  std::string save;

  for ( std::string::size_type i = 0; i < text.size(); i++ ) {
    if ( save.find ( text[i] ) == std::string::npos )
      save += text[i];
  }

  std::cout<< save <<'\n';
}

because its too easy o0`

Hello :
Thanks for all the answers......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.