Generate two output strings depending upon occurrence of character in input string . | CPP program to print two strings made of character occurring once and multiple times ; function to print two strings generated from single string one with characters occurring onces other with character occurring multiple of times ; initialize hashtable with zero entry ; perform hashing for input string ; generate string ( str1 ) consisting char occurring once and string ( str2 ) consisting char occurring multiple times ; print both strings ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 256 ; void printDuo ( string & str ) { int countChar [ MAX_CHAR ] = { 0 } ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) countChar [ str [ i ] - ' a ' ] ++ ; string str1 = " " , str2 = " " ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( countChar [ i ] > 1 ) str2 = str2 + ( char ) ( i + ' a ' ) ; else if ( countChar [ i ] == 1 ) str1 = str1 + ( char ) ( i + ' a ' ) ; } cout << " String ▁ with ▁ characters ▁ occurring ▁ " << " once : STRNEWLINE " ; cout << str1 << " STRNEWLINE " ; cout << " String ▁ with ▁ characters ▁ occurring ▁ " << " multiple ▁ times : STRNEWLINE " ; cout << str2 << " STRNEWLINE " ; } int main ( ) { string str = " lovetocode " ; printDuo ( str ) ; return 0 ; }