Created
April 3, 2020 22:29
-
-
Save asa55/c3415c89cc99a9c472f2c8de39a147a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// How do you print duplicate characters from a string? | |
#include <iostream> | |
#include <string> | |
#include <set> | |
std::string my_string = "hello world"; | |
std::string your_string = "bye world"; | |
std::set<char> my_set; | |
auto main () -> int | |
{ | |
int i, j; | |
for ( i = 0; i < my_string.size()-1; ++i ) | |
for ( j = i+1; j < my_string.size(); ++j ) | |
if ( my_string[i] == my_string[j] ) | |
my_set.insert(my_string[j]); // the loops find the duplicate elements and the set filters out ones we already have | |
for ( char el : my_set ) | |
std::cout << el << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment