
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- 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 II in C++
- Remove all duplicates from a given string in C#
- Identify all duplicates irrespective of the order of the input
- Print a closest string that does not contain adjacent duplicates in C++
- Remove All Adjacent Duplicates In String in Python
- Print all the combinations of a string in lexicographical order in C++
- Print all subsequences of a string in C++
- Print all funny words in a string in C++
- Print all the palindromic permutations of given string in alphabetic order in C++
- Remove all duplicates from a given string in Python
- Print all palindromic partitions of a string in C++
- Print all palindrome permutations of a string in C++
- Find All Duplicates in an Array in C++

Advertisements