C++ Program to find duplicate characters in a string


In this program, You will learn how to find duplicate characters in a string in C++.


The string is: madam

duplicate char is: ma

Example: How to find duplicate characters in a string in C++.

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

int main() {

    int i, j, len, v = 1;
    char str[100];

    cout << "Enter string:";
    cin.getline(str, 100);

    len = strlen(str);

    cout << "Duplicate characters:";
    for (i = 0; i < len; i++) {
        for (j = i + 1; j < len; j++) {
            if (str[i] == str[j]) {
                if (v == 1 && str[j] != '\0') {
                    cout << str[i];
                }
                str[j] = '\0';
                v++;
            }
        }
        v = 1;
    }

    return 0;
}

Output:

Enter string:welcome xiith
Duplicate characters:ei