### Comparing two strings with another string

1.Take input for universal set as a string from console U={1,2,9,56,a,Z,99.5} //how to read the string by ignoring {}, etc?
2.Take input for another set A as a string from console A={1,2,a,Z}
3.Take input for another set B as a string from console B={99,56,a,Z}
4.Check if A & B are subset of U
5.If not the program should print an error message "A or B isn't a subset of U"

6.If A & B are subset of universal set U then do union,intersection,disjoint,difference,complement between A and B.
Last edited on
What have you tried so far?
#include<iostream>
using namespace std;
int main()
{

char U[10000]; //Array for universal set
char A[10000]; //Array for first set
char B[10000]; //Array for second set
char R[10000]; //Array for result

int m;//Variable for first set element size.
int n;//Variable for second set element size.
int i,j;
int l=0; //Variable for result size.

char G='y';
int h;
while (G=='y')
{
cout<<"Please enter 1 for Union set \n";
cout<<"Please enter 2 for Intersection set \n";
cout<<"Please enter 3 for Subtraction set \n";
cout<<"Please enter 4 for Cartesian product \n";
cout<<"Please enter 5 for Power set \n";

cin>>h;
if(h==1)
{
//Getting input for first sets element size.
cin>>m;
//Storing size in variable.
cout<<endl;
//getting new line
//Getting input for second sets element size.
cin>>n;
//Storing size in variable.
cout<<endl;
//getting new line
for(i=0;i<m;i++)
//loop for get input elements for first set.
{
cin>>A[i];
}
cout<<"Elements of your first set = { ";
for(i=0;i<m;i++)
//loop for get input elements for first set.
{
cout<<A[i];
if(i<m-1)
{
cout<<" , ";
}
}
//loop printing second set elements.
cout<<" }";
cout<<"\n\n";
for(i=0;i<n;i++)
{
cin>>B[i];
}
cout<<"Elements of your second set = { ";
for(i=0;i<n;i++)
{
cout<<B[i];
if(i<n-1)
{
cout<<" , ";
}
}
//loop printing second set elements.
cout<<" }";
cout<<"\n\n";
//compute union
for(i=0; i<m; i++)
{
R[i] = A[i];
l++;
}
int e = l;
for(i=0; i<n; i++)
{
int found=0;
for(int j=0; j<e; j++)
{
if (B[i] == R[j])
{
found = 1;
break;
}
}
if (found==0)
{
R[l] = B[i];
l++;
}
}
cout<<"A Union B : \n\n\t\t { ";
for(i = 0; i < l; i++)
{
cout<<R[i]<<" ";
if (i<R[i+1])
{
cout<<',';
}

}
cout<<"}"<<endl;
}
else if (h==2)
{
//Getting input for first sets element size.
cin>>m;
//Storing size in variable.
cout<<endl;
//getting new line
//Getting input for second sets element size.
cin>>n;
//Storing size in variable.
cout<<endl;
//getting new line
for(i=0;i<m;i++)
//loop for get input elements for first set.
{
cin>>A[i];
}
cout<<"Elements of your first set = { ";
for(i=0;i<m;i++)
//loop for get input elements for first set.
{
cout<<A[i];
if(i<m-1)
{
cout<<" , ";
}
}
//loop printing second set elements.
cout<<" }";
cout<<"\n\n";
for(i=0;i<n;i++)
{
cin>>B[i];
}
cout<<"Elements of your second set = { ";
for(i=0;i<n;i++)
{
cout<<B[i];
if(i<n-1)
{
cout<<" , ";
}
}
//loop printing second set elements.
cout<<" }";
cout<<"\n\n";
//compute intersection
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
if (A[i] == B[j])
{
B[j]=0;
R[l] = A[i];
l++;
}
}
}
cout << "A Intersection B = { ";
for(i=0; i<l; i++)
{
cout << R[i] << " ";
if (i<R[i+1])
{
cout<<',';
}
}
cout << "}" << endl;

}
else if(h==3)
{
//Getting input for first sets element size.
cin>>m;
//Storing size in variable.
cout<<endl;
//getting new line
//Getting input for second sets element size.
cin>>n;
//Storing size in variable.
cout<<endl;
//getting new line
for(i=0;i<m;i++)
//loop for get input elements for first set.
{
cin>>A[i];
}
cout<<"Elements of your first set = { ";
for(i=0;i<m;i++)
//loop for get input elements for first set.
{
cout<<A[i];
if(i<m-1)
{
cout<<" , ";
}
}
//loop printing second set elements.
cout<<" }";
cout<<"\n\n";
for(i=0;i<n;i++)
{
cin>>B[i];
}
cout<<"Elements of your second set = { ";
for(i=0;i<n;i++)
{
cout<<B[i];
if(i<n-1)
{
cout<<" , ";
}
}
//loop printing second set elements.
cout<<" }";
cout<<"\n\n";
for(i=0; i<m; i++)
{
int found=0;
for(j=0; j<n; j++)
{

if (A[i] == B[j])
{
found=1;
break;

}
}
if(found==0)
{
R[l]=A[i];
l++;

}

}

for(i=0; i<n; i++)
{
int found=0;
for(j=0; j<m; j++)
{

if (B[i] == A[j])
{
found=1;
break;

}
}
if(found==0)
{
R[l]=B[i];
l++;

}

}
cout << "A Subtraction B = { ";
for(i=0; i<l; i++)
{
cout << R[i] << " ";
if (i<R[i+1])
{
cout<<',';
}
}
cout << "}" << endl;
}
}
}

Is there any option to use built in functions to read a string and ignore the {},.#% etc ?
Not really, but you can use built-in algorithms to help.
http://en.cppreference.com/w/cpp/algorithm/remove

The following will remove the curly braces from the input string.

 ``12345678910111213141516171819202122`` ``````// Example program #include #include #include // std::remove_if bool is_curly_brace(char c) { return c == '{' || c == '}'; } void erase_curly_braces(std::string& str) { str.erase(std::remove_if(str.begin(), str.end(), is_curly_brace), str.end()); } int main() { std::string foo = "{1,2,9,56,a,Z,99.5}"; erase_curly_braces(foo); std::cout << foo << std::endl; }``````

To support more than just curly braces, either add more logical-OR conditions, or use std::isalpha
http://en.cppreference.com/w/cpp/string/byte/isalpha

Please use code tags... that means putting the code you paste inside of [code] [/code] blocks.

_____________________________

Once you remove the curly braces, you'll want to split the string up by treating it like a comma-separated list. This is a bit more complicated, but look at https://stackoverflow.com/a/236803 for a solution using C++ library functionality.
Last edited on
 ``123456789101112131415161718192021222324252627282930313233343536`` ``````#include #include using namespace std; set split( string S, string delim ) { string part; set result; for ( char c : S ) { if ( delim.find( c ) != string::npos ) { if ( part != "" ) result.insert( part ); part = ""; } else { part += c; } } if ( part != "" ) result.insert( part ); return result; } int main() { string delim = "{}, "; // Choose what you like as endstops, blank space or delimiters string U = "{ 1, 2,9,56,a,Z,99.5 }"; set Uset = split( U, delim ); cout << "The " << Uset.size() << " elements of U are:\n"; for ( string s : Uset ) cout << s << '\n'; }``````

 ```The 7 elements of U are: 1 2 56 9 99.5 Z a```

Use the standard library, perhaps (regular expressions for parsing, and standard algorithms for operations on sets).
Though there is the omnipresent danger that the archetypal career teacher believes that learning to use the standard library is definitely not part of learning C++.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556`` ``````#include #include #include #include #include #include #include #include std::set make_set( std::string str ) { static const std::regex trim_re( R"(\s*([\{\,\}])\s*)" ) ; static const std::regex set_re( R"([^\{\}\,]+)" ) ; // http://en.cppreference.com/w/cpp/regex/regex_replace str = std::regex_replace( str, trim_re, "\$1" ) ; // http://en.cppreference.com/w/cpp/regex/regex_token_iterator std::sregex_token_iterator begin( str.begin(), str.end(), set_re ), end ; return { begin, end } ; } bool is_subset( const std::set& a, const std::set& b ) { // http://en.cppreference.com/w/cpp/algorithm/includes return std::includes( a.begin(), a.end(), b.begin(), b.end() ) ; } std::set union_of( const std::set& a, const std::set& b ) { std::vector u ; // http://en.cppreference.com/w/cpp/algorithm/set_union // http://en.cppreference.com/w/cpp/iterator/back_inserter std::set_union( a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(u) ) ; return { u.begin(), u.end() } ; } // likewise, for intersection etc. use the set operations in // see 'Set operations (on sorted ranges)' in http://en.cppreference.com/w/cpp/algorithm int main() { const std::string U = " { Labrador Retriever , Doberman Pinscher, Bullmastiff, Airdale Terrier } " ; for( const auto& tok : make_set(U) ) std::cout << std::quoted(tok) << '\n' ; std::cout << '\n' ; std::cout << std::boolalpha << is_subset( make_set(U), { "Doberman Pinscher", "Airdale Terrier" } ) << '\n' // true << is_subset( make_set(U), { "Doberman Pinscher", "Siamese Cat", "Airdale Terrier" } ) << '\n' ; // false std::cout << '\n' ; for( const auto& tok : union_of( make_set(U), { "Doberman Pinscher", "Siamese Cat", "Airdale Terrier" } ) ) std::cout << std::quoted(tok) << '\n' ; }``````

http://coliru.stacked-crooked.com/a/7dec9c85bff50daf
http://rextester.com/FSJZ33451
Wow, nice stuff you two.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990`` ``````#include #include #include #include using namespace std; //====================================================================== template set operator +( const set &A, const set &B ) // Union { set result = A; for ( T e : B ) result.insert( e ); return result; } //====================================================================== template set operator *( const set &A, const set &B ) // Intersection (sorry, can't think of a better operator) { set result; for ( T e : A ) if ( B.count( e ) ) result.insert( e ); return result; } //====================================================================== template void listSet( const string &title, const set &S ) { cout<< title << '\n'; for ( T e : S ) cout << e << '\n'; cout << '\n'; } //====================================================================== set split( string S, string delim ) { string part; set result; for ( char c : S ) { if ( delim.find( c ) != string::npos ) { if ( part != "" ) result.insert( part ); part = ""; } else { part += c; } } if ( part != "" ) result.insert( part ); return result; } //====================================================================== int main() { string Ustring = "{1,2,9,56,a,Z,99.5 }"; string Astring = "{1,2,a,Z}"; string Bstring = "{99,56,a,Z}"; string delim = "{}, "; // Choose what you like as endstops, blank space or delimiters set U = split( Ustring, delim ); set A = split( Astring, delim ); set B = split( Bstring, delim ); listSet( "Set U:", U ); listSet( "Set A:", A ); listSet( "Set B:", B ); listSet( "Set AuB:", A + B ); listSet( "Set AnB:", A * B ); if ( U + A == U ) cout << "A is a subset of U" << endl; else cout << "A is not a subset of U" << endl; } //====================================================================== ``````

 ```Set U: 1 2 56 9 99.5 Z a Set A: 1 2 Z a Set B: 56 99 Z a Set AuB: 1 2 56 99 Z a Set AnB: Z a A is a subset of U```
Last edited on
Topic archived. No new replies allowed.