C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
how to remove duplicate chars from a string Posted by eatsoft on 17 Jul 2005 at 7:58 PM
hi there...

i have a problem here..how to remove duplicate chars from a string
example:
input AAA BBB CCC
output A B C

can anybody help me...
Report
Re: how to remove duplicate chars from a string Posted by Vilanye on 17 Jul 2005 at 8:07 PM
: hi there...
:
: i have a problem here..how to remove duplicate chars from a string
: example:
: input AAA BBB CCC
: output A B C
:
: can anybody help me...
:

the string::at(int index) function lets you get a hold of individual characters, so you need to have an array to hold all characters. Then make a loop and read each character in the string and compare them to what is in your other array.
Report
Re: how to remove duplicate chars from a string Posted by eatsoft on 17 Jul 2005 at 8:12 PM
: : hi there...
: :
: : i have a problem here..how to remove duplicate chars from a string
: : example:
: : input AAA BBB CCC
: : output A B C
: :
: : can anybody help me...
: :
:
: the string::at(int index) function lets you get a hold of individual characters, so you need to have an array to hold all characters. Then make a loop and read each character in the string and compare them to what is in your other array.
:

then how to remove the duplicated chars, can you show me some code..
thanks..

Report
Re: how to remove duplicate chars from a string Posted by AsmGuru62 on 18 Jul 2005 at 4:32 AM
: : : hi there...
: : :
: : : i have a problem here..how to remove duplicate chars from a string
: : : example:
: : : input AAA BBB CCC
: : : output A B C
: : :
: : : can anybody help me...
: : :
: :
: : the string::at(int index) function lets you get a hold of individual characters, so you need to have an array to hold all characters. Then make a loop and read each character in the string and compare them to what is in your other array.
: :
:
: then how to remove the duplicated chars, can you show me some code..
: thanks..
:
:
Here is a simplest way, but not the fastest one:
char* deldupchars (char* s) {
  char* dups = strdup (s);

  if (dups) {
    char* psrc = dups;
    char* pdest = s;
    char ch;

    pdest [0] = '\0';
    while ((ch = *psrc++) != '\0') {
      if (! strchr (pdest, ch)) {
        *pdest++ = ch;
      }
    }
    pdest [0] = '\0';
    free (dups);
  }
  return s;
}


Report
Re: how to remove duplicate chars from a string Posted by HK_MP5KPDW on 18 Jul 2005 at 5:17 AM
: hi there...
:
: i have a problem here..how to remove duplicate chars from a string
: example:
: input AAA BBB CCC
: output A B C
:
: can anybody help me...
:

C-style null terminated character array type of string OR C++ STL string container? If the duplicate characters are always going to be adjacent to each other and C++ is an option, then you could just call the STL unique function and then either following it along with a call to the erase member function (if using C++ STL string containers) or instead just setting a new NULL terminating character if using C-style strings.
Report
Re: how to remove duplicate chars from a string Posted by stober on 18 Jul 2005 at 6:37 AM
: hi there...
:
: i have a problem here..how to remove duplicate chars from a string
: example:
: input AAA BBB CCC
: output A B C
:
: can anybody help me...
:

Another solution. string str2 contains the final result.
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string str1 = "ABC ADC BCA";
	string str2;
	int pos;
	for(int i = 0; i < str1.length(); i++)
	{
		if( (pos = str2.find(str1[i])) < 0)
			str2 += str1[i];

	}
	cout << str2 << endl;
	return 0;
}

Report
Re: how to remove duplicate chars from a string Posted by eatsoft on 18 Jul 2005 at 6:55 PM
: : hi there...
: :
: : i have a problem here..how to remove duplicate chars from a string
: : example:
: : input AAA BBB CCC
: : output A B C
: :
: : can anybody help me...
: :
:
: Another solution. string str2 contains the final result.
:
: #include <iostream>
: #include <string>
: using namespace std;
: 
: int main()
: {
: 	string str1 = "ABC ADC BCA";
: 	string str2;
: 	int pos;
: 	for(int i = 0; i < str1.length(); i++)
: 	{
: 		if( (pos = str2.find(str1[i])) < 0)
: 			str2 += str1[i];
: 
: 	}
: 	cout << str2 << endl;
: 	return 0;
: }
: 

:


its really working, but the solution in C.what method can we use.
Report
Re: how to remove duplicate chars from a string Posted by stober on 18 Jul 2005 at 7:11 PM
I know you want us to do your homework for you. You should have enough hints to do it yourself. Post your code and ask questions. Hint: strchr() function will find a chacter in a string.
Report
Re: how to remove duplicate chars from a string Posted by oth8man on 19 Jun 2009 at 5:58 AM
//try to run this
//BY way can I ask you from where you got this question ?
#include<stdio.h>
#include<string.h>
char* remove(char* s)
{
char *temp;
while(*s)
{
if(strchr(temp,*s)==NULL)
strncat(temp,s,1);
s++;
}
return temp;
}
int main()
{
char str[100];
puts("str:");
gets(str);
puts(remove(str));
return 0;
}

Report
Re: how to remove duplicate chars from a string Posted by HK_MP5KPDW on 22 Jun 2009 at 6:12 AM
Almost 4 years is far too long to be bumping a post.
Report
Re: how to remove duplicate chars from a string Posted by oth8man on 28 Jun 2009 at 3:18 AM
hhhh ~~ wow
I didn't see the date,,,,Thanks 4 telling me



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.