Finding duplicate char from a string
Copying the string in step 3 is unnecessary

You can iterate through the characters of a std::string s using std::string::operator[] and std::string::size():

for (std::string::size_type pos = 0; pos &lt; s.size(); pos++) {
    char c = s[pos]; // do something with the char now
}


Alternatively, you can use std::string::iterator to iterate through the string.



Avoid std::endl in favor of \n

std::endl flushes the stream, which can cause a loss in performance.



Use more whitespace in your code

It's difficult to read code like for(int i=0;temp[i];++i). Instead, use more whitespace as in for (int i = 0; temp[i]; ++i).



I would take a different approach to solve the problem. With your approach, you have to iterate through the string at least once to sort it (step 1), then again to remove the whitespace (step 2), then a third time to look for duplicates. If you copy the string in step 3 that's yet another iteration.

You only need to iterate through the string once. Here's one way to do that:

Iterate through the string's characters (skipping the whitespace characters) and put each character in a std::set&lt;char&gt; if you haven't encountered it before. If you've already encountered it (it's in the std::set) put it in a separate std::set of duplicates. Once you've iterated through the string's character's once, you can iterate through the set of duplicates to print them.

Here's a demo:

#include &lt;iostream&gt; 
#include &lt;string&gt;
#include &lt;cctype&gt;
#include &lt;set&gt;

int main() {
    std::string s("Hello world");
    std::set&lt;char&gt; characters;
    std::set&lt;char&gt; duplicates;

    for (std::string::size_type pos = 0; pos &lt; s.size(); pos++) {
        char c = s[pos];

        // std::isspace() accepts an int, so cast c to an int
        if (!std::isspace(static_cast&lt;int&gt;(c))) {
            if (characters.count(c) == 0) {
                characters.insert(c);
            } else {
                duplicates.insert(c);
            }
        }
    }

    std::cout &lt;&lt; "Duplicates: ";

    for (std::set&lt;char&gt;::const_iterator it = duplicates.begin(); it != duplicates.end(); ++it) {
        std::cout &lt;&lt; *it &lt;&lt; " ";
    }

    return 0;
}


Note that it's better to use std::isspace() instead of ' ' to check if a character is whitespace.

If you need to count how many times each character occurs in the string, simply replace std::set&lt;char&gt; duplicates with a std::map&lt;char, int&gt; which maps each character to the number of times it occurs (as shown in @TobySpeight's answer).