convert a bool return value into YES or NO

The following program finds whether the given two strings are isomorphic or not.
This code returns 1 or 0 but i want to return "Yes" and "No" as output.

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
// C++ program to check if two strings are isomorphic
#include <iostream>
#include <cstring>
#include <string.h>
using namespace std;
#define MAX_CHARS 256
 
// This function returns true if str1 and str2 are ismorphic
bool areIsomorphic(string str1, string str2)
{
 
    int m = str1.length(), n = str2.length();
 
    // Length of both strings must be same for one to one
    // corresponance
    if (m != n)
      return false;
 
    // To mark visited characters in str2
    bool marked[MAX_CHARS] = {false};
 
    // To store mapping of every character from str1 to
    // that of str2. Initialize all entries of map as -1.
    int map[MAX_CHARS];
    memset(map, -1, sizeof(map));
 
    // Process all characters one by on
    for (int i = 0; i < n; i++)
    {
        // If current character of str1 is seen first
        // time in it.
        if (map[str1[i]] == -1)
        {
            // If current character of str2 is already
            // seen, one to one mapping not possible
            if (marked[str2[i]] == true)
                return false;
 
            // Mark current character of str2 as visited
            marked[str2[i]] = true;
 
            // Store mapping of current characters
            map[str1[i]] = str2[i];
        }
 
        // If this is not first appearance of current
        // character in str1, then check if previous
        // appearance mapped to same character of str2
        else if (map[str1[i]] != str2[i])
            return false;
    }
 
    return true;
}
 
// Driver program
int main()
{
   char arr1[1000];
   char arr2[1000];
   cin>>arr1;
   cin>>arr2;
   cout << areIsomorphic(arr1,arr2);
   return 0;
}
Last edited on
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
// C++ program to check if two strings are isomorphic
#include <iostream> // this is a standard C++ header (std::cout etc.)
#include <string> // this too is a standard C++ header (std::string)
#include <algorithm> // this too is a standard C++ header (std::sort)

// This function returns true if str1 and str2 are ismorphic
bool areIsomorphic( std::string str1, std::string str2 )
{
    // sort the two strings
    std::sort( str1.begin(), str1.end() ) ;
    std::sort( str2.begin(), str2.end() ) ;

    // the input strings are isomorphic if the sorted strings are equal
    return str1 == str2 ;
}

// Driver program
int main()
{
    std::string one ;
    std::string two ;

    std::cin >> one >> two ;

    const bool isomorphic = areIsomorphic( one, two ) ;
    std::cout << "isomorphic? " << ( isomorphic ? "YES" : "NO" ) << '\n' ;
}
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
#include <iostream>
#include <string>
using namespace std;

const int MAXCHARS = 256;    // not sure if portable; probably fixable if not


bool areIsomorphic( const string& s1, const string& s2 )
{
   if ( s1.size() != s2.size() ) return false;

   int tally[MAXCHARS] = { 0 };
   for ( unsigned char c : s1 ) tally[c]++;
   for ( unsigned char c : s2 ) tally[c]--;
   for ( int i = 0; i < MAXCHARS; i++ ) if ( tally[i] != 0 ) return false;
   return true;
}


int main()
{
   const string ans[2] = { "NO", "YES" };
   string s1, s2;

   cout << "Enter s1: ";   getline( cin, s1 );
   cout << "Enter s2: ";   getline( cin, s2 );
   cout << "Isomorphic? " << ans[ areIsomorphic( s1, s2 ) ];
}
Last edited on
The solutions so far aren't in line with description of isomorphic strings found elsewhere:

http://yucoding.blogspot.in/2015/06/leetcode-question-isomorphic-strings.html
http://www.geeksforgeeks.org/check-if-two-given-strings-are-isomorphic-to-each-other/

so, first things first, can we agree what is an isomorphic string?

edit: OP, your original code seems to be in line with the isomorphic string definitions elsewhere (subject to further confirm). So if you just want to print 'yes' and 'no' for true and false respt, here's an augmented version of your program: http://cpp.sh/3cc7z
Last edited on
Ah yes, you are right on the definition @gunnerfunner. I misread it.
I'll try again. Aim is to make it work for any container, not just strings.

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
#include <iostream>
#include <map>
#include <string>
using namespace std;


template <typename C> bool areIsomorphic( const C& s1, const C& s2 )  // any container C (not just strings)
{
   if ( s1.size() != s2.size() ) return false;                        // different sizes? forget it
   size_t size = s1.size();

   using T = typename C::value_type;
   map<T,T> map12, map21;                                             // forward and backward maps; container element types
   T c1, c2;                                                          // individual elements

   for ( size_t i = 0; i < size; i++ )
   {
      c1 = s1[i];
      c2 = s2[i];
      if ( map12.count( c1 ) && map12[c1] != c2 ) return false;       // already used c1 but doesn't match target
      if ( map21.count( c2 ) && map21[c2] != c1 ) return false;       // already used c2 but doesn't match target
      map12[c1] = c2;                                                 // new (or redundant) mapping
      map21[c2] = c1;                                                 // new (or redundant) back-mapping
   }
   return true;
}


int main()
{
   const string ans[2] = { "NO", "YES" };
   string s1, s2;

   cout << "Enter s1: ";   getline( cin, s1 );
   cout << "Enter s2: ";   getline( cin, s2 );
   cout << "Isomorphic? " << ans[ areIsomorphic( s1, s2 ) ] << endl;
}

Topic archived. No new replies allowed.