How to find duplicate characters in a string in simple steps?

How to find duplicate characters in a string in simple steps?

Find duplicate characters in a string

The simplest way of finding duplicate characters can be achieved in below-mentioned steps:

Step 1. Iterate through the string

Step 2. Maintain a counter, which will hold duplicate characters based on ASCII numbers.

Step 3. Each time when you iterate, insert character according to ASCII number.

#define NO_OF_CHARS 256
void CountDuplicateCharacters(char *str){
   int *count;
  count = new int[NO_OF_CHARS];

  for (int i = 0; *(str + i); i++)
  {
      count[*(str + i)]++;
  }


  for (int i = 0; i < NO_OF_CHARS; i++)
     if(count[i] > 1)
        printf("%c\t\t %d \n", i, count[i]);
  delete[] count;
}


int main(){
char str[] = “Welcome to NectarPost”;
cout<<“duplicate characters in given string are\n”;
cout<<“character\tcount\n”;
CountDuplicateCharacters(str);
return 0;
}

Leave a Reply