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)
{
cout<<"Please enter number of elements of your first set: ";
//Getting input for first sets element size.
cin>>m;
//Storing size in variable.
cout<<endl;
//getting new line
cout<<"Please enter number of elements of your second set: ";
//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.
{
cout<<"Please enter element no "<<i+1<<" of your 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++)
{
cout<<"Please enter element no "<<i+1<<" of your second set: ";
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)
{
cout<<"Please enter number of elements of your first set: ";
//Getting input for first sets element size.
cin>>m;
//Storing size in variable.
cout<<endl;
//getting new line
cout<<"Please enter number of elements of your second set: ";
//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.
{
cout<<"Please enter element no "<<i+1<<" of your 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++)
{
cout<<"Please enter element no "<<i+1<<" of your second set: ";
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)
{
cout<<"Please enter number of elements of your first set: ";
//Getting input for first sets element size.
cin>>m;
//Storing size in variable.
cout<<endl;
//getting new line
cout<<"Please enter number of elements of your second set: ";
//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.
{
cout<<"Please enter element no "<<i+1<<" of your 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++)
{
cout<<"Please enter element no "<<i+1<<" of your second set: ";
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Example program
#include <iostream>
#include <string>

#include <algorithm> // 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <set>
using namespace std;


set<string> split( string S, string delim )
{
   string part;
   set<string> 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<string> 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++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
#include <regex>
#include <set>
#include <algorithm>
#include <vector>
#include <iterator>
#include <iomanip>

std::set<std::string> 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<std::string>& a, const std::set<std::string>& b )
{
    // http://en.cppreference.com/w/cpp/algorithm/includes
    return std::includes( a.begin(), a.end(), b.begin(), b.end() ) ;
}

std::set<std::string> union_of( const std::set<std::string>& a, const std::set<std::string>& b )
{
    std::vector<std::string> 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 <algorithm>
// 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <set>
#include <iterator>
#include <algorithm>
using namespace std;


//======================================================================


template <typename T> set<T> operator +( const set<T> &A, const set<T> &B )    // Union
{
   set<T> result = A;
   for ( T e : B ) result.insert( e );
   return result;
}


//======================================================================


template <typename T> set<T> operator *( const set<T> &A, const set<T> &B )    // Intersection (sorry, can't think of a better operator)
{
   set<T> result;
   for ( T e : A ) if ( B.count( e ) ) result.insert( e );
   return result;
}


//======================================================================


template <typename T> void listSet( const string &title, const set<T> &S )
{
   cout<< title << '\n';
   for ( T e : S ) cout << e << '\n';
   cout << '\n';
}


//======================================================================


set<string> split( string S, string delim )
{
   string part;
   set<string> 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<string> U = split( Ustring, delim );
   set<string> A = split( Astring, delim );
   set<string> 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.