
- 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 distinct characters of a string in order in C++
In this problem, we are given a string. Our task is to print all distinct characters of the string in the order they appear in the string.
Let’s take an example to understand our problem,
Input: tutorials Point Output: uralsPn
There are multiple ways to solve this problem but we will discuss the most effective one. The simple one includes the nesting of loops.
For this, we will use two arrays of size 256(8-bit characters are stored).
First we will initialize all values of counter array to 0 and all values of index array to n (length of string). On traversal of the string str and for every character c, increase count[x], if count[x] = 1, index[x] = i. If count[x] = 2, index[x] = n. Sort indexes and print characters.
Example
The code to show the implementation of our solution,
#include <bits/stdc++.h> using namespace std; const int MAX_CHAR = 256; void printDistinctCharacters(string str) { int n = str.length(); int count[MAX_CHAR]; int index[MAX_CHAR]; for (int i = 0; i < MAX_CHAR; i++) { count[i] = 0; index[i] = n; } for (int i = 0; i < n; i++) { char x=str[i]; ++count[x]; if (count[x] == 1 && x !=' ') index[x] = i; if (count[x] == 2) index[x] = n; } sort(index, index+MAX_CHAR); for (int i=0; i<MAX_CHAR && index[i] != n; i++) cout<<str[index[i]]<<" "; } int main() { string str = "tutorialsPoint"; cout<<"All distinct Characters of the string '"<<str<<"' are :\n"; printDistinctCharacters(str); return 0; }
Output
All distinct Characters of the string 'tutorialsPoint' are − u r a l s P n
- Related Articles
- Find distinct characters in distinct substrings of a string
- Print all distinct permutations of a given string with duplicates in C++
- Print all distinct circular strings of length M in lexicographical order in C++
- Print all the combinations of a string in lexicographical order in C++
- How to print all the characters of a string using regular expression in Java?
- Print all the palindromic permutations of given string in alphabetic order in C++
- Python Program to Print All Permutations of a String in Lexicographic Order without Recursion
- Python Program to Print All Permutations of a String in Lexicographic Order using Recursion
- Print All Distinct Elements of a given integer array in C++
- Longest string with two distinct characters in JavaScript
- Print common characters of two Strings in alphabetical order in C++
- Print characters having odd frequencies in order of occurrence in C++
- Print characters and their frequencies in order of occurrence in C++
- Java Program to print distinct permutations of a string
- Print all subsequences of a string in C++

Advertisements