Finding duplicate char from a string I have written code to find a duplicate char from a string. Can the code be improved?

Input: 

         Hello World


output : 

         l =  3 its a dup

         o = 2 its a dup


My approach is 

step 1: sort the string 

step 2: remove all the whitespace

step 3: copy std::string to char [] and loop through the array until different char and count on every dup char if count is more than one its duplicate.

#include&lt;iostream&gt;
#include&lt;algorithm&gt;
#include&lt;set&gt;
#include&lt;string&gt;
#include&lt;string.h&gt;
#include&lt;unistd.h&gt;

void dupcheck(std::string usr)
{
        int count =1;
        char temp[1024]={0};
        strncpy(temp,usr.c_str(),sizeof(temp));
        for(int i=0;temp[i];++i)
        {
                for(int j=i+1;temp[j]==temp[i];++j)
                {
                        if(temp[i]==temp[j])
                                count++;
                                i=j;

                }
                        if(count&gt;=2)
                        {
                                std::cout&lt;&lt;"dup char ="&lt;&lt;temp[i]&lt;&lt;" "&lt;&lt;count&lt;&lt;std::endl;
                                count =1;
                        }
        }

}

int main()
{
        std::string userInput;
        std::getline(std::cin,userInput);            
        userInput.erase(std::remove(userInput.begin(), userInput.end(),' '), userInput.end());
        std::sort(userInput.begin(),userInput.end());
        std::cout&lt;&lt;userInput&lt;&lt;std::endl;
        if(!userInput.empty())
        dupcheck(userInput);
}


Please help me to improve the code if possible.
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).