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.
// C++ program to check if two strings are isomorphic
#include <iostream>
#include <cstring>
#include <string.h>
usingnamespace 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)
returnfalse;
// 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)
returnfalse;
// 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
elseif (map[str1[i]] != str2[i])
returnfalse;
}
returntrue;
}
// Driver program
int main()
{
char arr1[1000];
char arr2[1000];
cin>>arr1;
cin>>arr2;
cout << areIsomorphic(arr1,arr2);
return 0;
}
// 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 ;
constbool isomorphic = areIsomorphic( one, two ) ;
std::cout << "isomorphic? " << ( isomorphic ? "YES" : "NO" ) << '\n' ;
}
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