program that outputs the different characters (not the ones in common) between 2 strings that read all characters in c++ -


#include<iostream> #include<cstdlib> #include<cctype> #include <iomanip> #include <cstring> //---------------------- using std :: cin; using std :: cout; using std :: endl; using std::setw ; //------------------- const int max_str_len=100; //----------------------- int main() {   char str1 [max_str_len];   char str2 [max_str_len];   int i;   cin >> setw(max_str_len)>>str1;   cin >> setw(max_str_len)>>str2;        if (strcmp(str1, str2)!=0)       {         (int i=0; < strlen(str1); i++)         {            (int j=0; j < strlen(str2); j++)            {              if (str1[i]==str2[j]) // want check whats common first                                    // , cout whats left (not common)              {                    cout << str2[j];              }         }     }    return exit_success; } 

this works fine if i'm looking common characters. have tried loop : if (str1[i]!=str2[j]) since it's in for loops gave me difference between i , of string j. example if str1=abc , str2=abcd answer should d

if may not change strings can use approach std::set container. example

#include <iostream> #include <string> #include <set> #include <algorithm> #include <iterator>  int main() {     std::string s1( "abd" );     std::string s2( "ace" );      std::set<char> set1( s1.begin(), s1.end() );     std::set<char> set2( s2.begin(), s2.end() );      std::set_symmetric_difference( set1.begin(), set1.end(), set2.begin(), set2.end(),                                   std::ostream_iterator<char>( std::cout ) );     std::cout << std::endl; } 

the output is

bcde 

at least program gives expected result d strings abc , abcd.:)

otherwise sort strings , apply same standard algorithm std::set_symmetric_difference.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -