
- 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
C++ program to find uncommon characters in two given strings
In this article, we will be discussing a program to find out the uncommon characters during comparison of two different given strings.
As we know, strings are nothing but an array of characters. Therefore, for comparison we would be traversing through the characters of one string and simultaneously checking if that element exists in the other string.
If we let the first string be A and the second string B.Then it would give us A - B. Similarly we can calculate B - A.
Combining both of these results we would get
( A - B ) ∪ ( B - A )
i.e the uncommon elements among both the strings.
Example
#include <iostream> using namespace std; int main() { int len1 = 5, len2 = 4; char str1[len1] = "afbde", str2[len2] = "wabq"; cout << "Uncommon Elements :" <<endl; //loop to calculate str1- str2 for(int i = 0; i < len1; i++) { for(int j = 0; j < len2; j++) { if(str1[i] == str2[j]) break; //when the end of string is reached else if(j == len2-1) { cout << str1[i] << endl; break; } } } //loop to calculate str2- str1 for(int i = 0; i < len2; i++) { for(int j = 0; j < len1; j++) { if(str2[i] == str1[j]) break; else if(j == len1-1) { cout << str2[i] << endl; break; } } } return 0; }
Output
Uncommon Elements : f d e w q
- Related Articles
- Find uncommon characters of the two strings in C++ Program
- Find uncommon characters of the two strings in C++
- Python program to find uncommon words from two Strings
- Finding and returning uncommon characters between two strings in JavaScript
- Program to find uncommon elements in two arrays - JavaScript
- Concatenated string with uncommon characters in Python program
- Python Program to print all distinct uncommon digits present in two given numbers
- Golang Program to find the uncommon elements from two arrays
- Python Program – Strings with all given List characters
- Program to find size of common special substrings of two given strings in Python
- Program to equal two strings of same length by swapping characters in Python
- Concatenated string with uncommon characters in Python?
- Find the uncommon values concatenated from both the strings in Java
- C program to find permutations of given strings
- Count common characters in two strings in C++

Advertisements