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 character is: ma


Example: How to find duplicate characters in a string in c

Run it Now
#include<stdio.h>
#include<string.h>

int main() {

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

    printf("Enter String :");
    fgets(str, 100, stdin);

    len = strlen(str);

    printf("\nDuplicate 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') {
                    printf("%c", str[i]);
                }
                str[j] = '\0';
                v++;
            }
        }
        v = 1;
    }

    return 0;
}

Output:

Enter String :teeesst
Duplicate Characters :tes