
- 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 common characters of two Strings in alphabetical order in C++
In this programming problem, we are given two strings. And we have to find all the characters of the string that are common in both the string and we have to print these common characters in alphabetical order. And print ‘NO COMMON CHARACTERS ’ found if no common letters arise. Given that the string does not contain all lower case alphabets.
Let’s take an example −
Input : string1 : adsfhslf string2 : fsrakf Output : affs
Explanation − Between the two strings there are a, f, s. Hence the lexicographical output is ‘afs’.
Input : string1 : abcde string2 : glhyte Output : No common characters
Explanation − There are no characters are in common.
To solve this problem, we need to find common characters in the string. And the output will be the lexicographical order of these string.
Algorithm
Algorithm to solve this problem is −
Step 1 : Create two arrays a1[] and a2[] of size 26 each for counting the number of alphabets in the strings string1 and string2. Step 2 : traverse a1[] and a2[]. and in sequence print all those numbers that have values in the array.
Example
Let's create a program based on this algorithm to illustrate the working −
#include<bits/stdc++.h> using namespace std; int main(){ string string1 = "adjfrdggs"; string string2 = "gktressd"; cout<<"The strings are "<<string1<<" and "<<string2; cout<<"\nThe common characters are : "; int a1[26] = {0}; int a2[26] = {0}; int i , j; char ch; char ch1 = 'a'; int k = (int)ch1, m; for(i = 0 ; i < string1.length() ; i++){ a1[(int)string1[i] - k]++; } for(i = 0 ; i < string2.length() ; i++){ a2[(int)string2[i] - k]++; } for(i = 0 ; i < 26 ; i++){ if (a1[i] != 0 and a2[i] != 0){ for(j = 0 ; j < min(a1[i] , a2[i]) ; j++){ m = k + i; ch = (char)(k + i); cout << ch; } } } return 0; }
Output
The strings are adjfrdggs and gktressd The common characters are : dgrs
- Related Articles
- Python code to print common characters of two Strings in alphabetical order
- Java code to print common characters of two Strings in alphabetical order
- Count common characters in two strings in C++
- Python – Extract Strings with Successive Alphabets in Alphabetical Order
- Removing n characters from a string in alphabetical order in JavaScript
- Sorting numbers in ascending order and strings in alphabetical order in an array in JavaScript
- Sort the array of strings according to alphabetical order defined by another string in C++
- Print all distinct characters of a string in 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++
- Order strings by length of characters IN mYsql?
- Find uncommon characters of the two strings in C++
- Check if the characters of a given string are in alphabetical order in Python
- C program to print characters and strings in different formats.
- Count common subsequence in two strings in C++
