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++.


String is :madam

duplicate char is: ma


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

Run it Now
#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 << "\nDuplicate Character List is :";
    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 :teeesst
Duplicate Character List is :tes