

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print all the duplicates in the input string in C++
In this problem, we are given a string and we have to find all the characters that are duplicated along with their number of occurrences in the string.
Let’s take an example to understand the problem −
Input: TutorialsPoint Output: t (3) o (2) i (2)
Explanation− The frequencies of occurrence of each character are t → 3; u → 1; o → 2; r → 1; i → 2; a → 1; s → 1; n → 1.
Now, to solve this problem we will find the character count and store it in an array from the string. And then print the characters and occurrences where freq. It is more than 1.
Example
# include <iostream> using namespace std; # define NO_OF_CHARS 256 class duplicate_char{ public : void charCounter(char *str, int *count){ int i; for (i = 0; *(str + i); i++) count[*(str + i)]++; } void printDuplicateCharacters(char *str){ int *count = (int *)calloc(NO_OF_CHARS, sizeof(int)); charCounter(str, count); int i; for (i = 0; i < NO_OF_CHARS; i++) if(count[i] > 1) printf("%c\t\t %d \n", i, count[i]); free(count); } }; int main(){ duplicate_char dupchar ; char str[] = "tutorialspoint"; cout<<"The duplicate characters in the string\n"; cout<<"character\tcount\n"; dupchar.printDuplicateCharacters(str); return 0; }
Output
The duplicate characters in the string character count
i 2 o 2 t 3
- Related Questions & Answers
- Identify all duplicates irrespective of the order of the input
- Print all distinct permutations of a given string with duplicates in C++
- Print distinct sorted permutations with duplicates allowed in input in C++
- Remove All Adjacent Duplicates In String in Python
- Remove All Adjacent Duplicates in String II in C++
- Remove all duplicates from a given string in C#
- Remove all duplicates from a given string in Python
- Print all the combinations of a string in lexicographical order in C++
- Print a closest string that does not contain adjacent duplicates in C++
- Print all the palindromic permutations of given string in alphabetic order in C++
- Print all subsequences of a string in C++
- Print all permutations of a string in Java
- Print all funny words in a string in C++
- How can we print all the capital letters of a given string in Java?
- How to print all the characters of a string using regular expression in Java?
Advertisements