Anybody know how to remove the duplicate characters in turbo c++.
the output is like this:

enter string1:abc
enter string2:bcd

Re: removing duplicate character in turbo c++ 80 80

There are lots of ways that you could do this, but you'd have to say more about the rules that you have to follow. For example, is the order of the characters important? Do they always have to remain in a string? Are you allowed to use STL algorithms?

With just what you have said so far, there are at least two easy ways to do this, one using std::set< char > and the other using std::vector< char > . With set :

#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <set>

int main()
    int numberOfStringsToCombine = 2;
    std::set< char > uniqueChars;

    for ( int i = 0; i < numberOfStringsToCombine; ++i ){
        /* Read in a string from the user */
        std::string s;
        std::cout << "Enter a string: ";
        std::cin >> s;

        /* Try and insert the characters in the string into the set */
        uniqueChars.insert( s.begin(), s.end() );

    /* Write the characters in the set to stdout */
    std::cout << "Unique chars across all strings: ";
    std::copy( uniqueChars.begin(), uniqueChars.end(), std::ostream_iterator< char >( std::cout) );
    std::cout << std::endl;

    return 0;

This approach relies on the fact that std::set can only contain one of each type of object, so when you use insert to add the characters, it's automatically checked that no duplicates are added. As a bonus, you get the letters sorted alphabetically at the end too :o)

The approach using vector would be similar, but you would use std::sort and then std::unique . In some circumstances this could be a lot more efficient.

Re: removing duplicate character in turbo c++ 80 80

Anybody know how to remove the duplicate characters in turbo c++.

There are a number of ways. The brute force method is the probably simplest, where you copy each character that isn't in the other string to a result string:

function set_difference(str1, str2)
    result = ""

    for each c1 in str1
        matched = false

        for each c2 in str2
            if c1 = c2
                matched = true

        if not matched
            result.append c1

    return result

a = "abc"
b = "bcd"
print set_difference(a, b) + set_difference(b, a)

Of course, brute force can be problematic in terms of growth. The longer the strings, the slower the algorithm. Ideally you'd have an algorithm that makes one pass over each string.

The operation you're looking for is called a symmetric difference in set theory. It results in items that are in either set, but not both. A list based XOR, if you will. Assuming both sets are sorted, the single pass algorithm is actually quite simple:

function symmetric_difference(str1, str2)
    result = ""

    for i = 0, j = 0 to str1.len, str2.len
        if str1[i] < str2[j]
            result.append str1[i]
            i = i + 1
        else if str2[j] < str1[i]
            result.append str2[j]
            j = j + 1
            i = i + 1
            j = j + 1

    while i < str1.len
        result.append str1[i]
        i = i + 1

    while j < str2.len
        result.append str2[j]
        j = j + 1

    return result

print symmetric_difference("abc", "bcd")

If you're familiar with merging, you'll see that this algorithm is a variation. The translation to C++ (even the prestandard C++ of your compiler) is straightforward.

Are you allowed to use STL algorithms?

I think a better question would be is he using a version of Turbo C++ that supports the STL at all? From previous questions, the answer to that is probably no. So at best we can only assume a more manual programming style than modern programmers should be used to. ;)

Re: removing duplicate character in turbo c++ 80 80

STL algorithm is not allowed on my program...
what is the best code for these???

Re: removing duplicate character in turbo c++ 80 80

std::ostream_iterator< char >

may know how thus this work???

Re: removing duplicate character in turbo c++ 80 80

I think this is how that 2nd piece of code you posted works,with "abc" and "bcd":
First time,it would go into lines 6 and 7,leaving i=1 and and j=0.
2nd time,it would do the else block(lines 12 and 13),
leaving i=2 and j=1.
3rd time,it would again do the else block
leaving i=3,j=2.
the next time,there should be an out of bounds error.

Am I wrong?

Be a part of the DaniWeb community

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