Aug 082012

We have already seen the problem to print first repeating character in a string. In this post we will be looking at printing all the repeating characters in the string. For example:

Input String: ""         Output: w.ria
Input String: ""      Output: ia.r
Input String: "meenakshi"                 Output: e 

Note that we are printing the repeating character only once.

We will be extending the second method in the solution to printing the first repeating character in the string.

Step-1: Create the count array. 
        which will hold the number of times a character is repeating in the string.
    For each element of the string
        If count is >1
            print the character
            negate the count (multiply by -1) //so that it doesn't get printed again


void printRepeating(char* str)
    // Boundary check
    if(str == NULL || str[0] == '')
        printf("EMPTY STRING");

    // Count Array. All elements are zeros
    int cntArr[256] = {0};

    // Populating the Count Array
    for(int i = 0; str[i] != '';  i++)

    // Getting the index of Maximum count in the Array
    int maxCntIdx = 0;
    for(int i=0; str[i] != '';  i++)
        if(cntArr[str[i]] > 1)
            printf(" %c ", str[i]);
            cntArr[str[i]] *= -1;

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>