Print Duplicate Characters in String

Code Snippets 4 U
#include < stdio.h > 
#include < stdlib.h > 
#include < string.h >
  int main() {
    char * str = "Hi I am Good and Printing Duplicate";
    // for ASCII characters only
    static int strCount[256];
    // count the total occurences
    for (int i = 0; i < strlen(str); i++) {
      ++strCount[ * (str + i)];
    }
    // count is stored at the place of respective ASCII value Index
    for (int i = 0; i < 256; i++) {
      if (strCount[i] > 1) {
        printf("'%c',", i);
      }
    }
    return 0;
  }

Leave a Reply

Your email address will not be published. Required fields are marked *

9 + one =