- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find uncommon characters of the two strings in C++ Program
In this tutorial, we are going to learn how to find distinct characters from the given two strings. Let's see an example.
Input
string_one = "tutorialspoint" string_two = "tutorialsworld"
Output
d n p w
We are going to use hashing to solve the problem. It's more efficient than writing two nested loop
Let's see the steps to solve the program.
Initialize the two strings with some random values.
Initialize a map as map<char, int> chars.
Iterate over the first string and insert each character into map with value 1.
Now, iterate over the second string.
Check whether the character is already present or not.
If present, assign 0 to it.
If not present insert the character with value 1.
Iterate over the map and print characters with value 1.
Example
See the code below.
#include <bits/stdc++.h> #include <map> using namespace std; void findDistinctCharacters(string one, string two){ // initializing char presence in string map<char, int> chars; // iterating over the first string for (int i = 0; i < one.size(); ++i){ // inserting every character into map chars.insert({one[i], 1}); } // iterating over the second string for (int i = 0; i < two.size(); ++i){ // checking whether the current char in string or not if (chars.count(two[i])) { // assigning 0 for common chars chars.find(two[i])->second = 0; } else { // insering new chars chars.insert({two[i], 1}); } } // printing the distinct characters for (auto item: chars){ // checking the presence if (item.second == 1) { // printing the distinct char cout << item.first << " "; } } } int main(){ string one = "tutorialspoint"; string two = "tutorialsworld"; findDistinctCharacters(one, two); return 0; }
Output
If you run the above code, you will get the following result.
d n p w
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Find uncommon characters of the two strings in C++
- C++ program to find uncommon characters in two given strings
- Finding and returning uncommon characters between two strings in JavaScript
- Python program to find uncommon words from two Strings
- Concatenated string with uncommon characters in Python program
- Program to find uncommon elements in two arrays - JavaScript
- Count common characters in two strings in C++
- Find the uncommon values concatenated from both the strings in Java
- Print common characters of two Strings in alphabetical order in C++
- Program to equal two strings of same length by swapping characters in Python
- Concatenated string with uncommon characters in Python?
- Program to find the minimum edit distance between two strings in C++
- C program to print characters and strings in different formats.
- An Uncommon representation of array elements in C++ program
- Program to find largest merge of two strings in Python
