Check if two given strings are isomorphic

Here given code implementation process.

// C Program
// Check if two given strings are isomorphic
#include<stdio.h>



void is_isomorphic(char str1[],char str2[],int s1,int s2)
{

  int status=1;
  
  if(s1!=s2)
  {
    //When given string lengths are not equal
    status = 0;
  }
  for (int i = 0; i < s1 && status==1; ++i)
  {
    
    for (int j = i; j < s2 && status==1; ++j)
    {
      //compare location [i] character are similar to another string or not
      if(str1[i] == str1[j] && str2[i] != str2[j])
      {
        status=0;
      }
    } 
  }
  printf("%s : %s\n",str1,str2 );
  if(status==1)
  {

    printf("Isomorphic\n");
  }
  else
  {
    printf("Not Isomorphic\n");
  }
}

int main()
{
  
  char str1[] = "Codepaper";
  char str2[] = "minkdidkt";

  //Get the size
  int size1=sizeof(str1)/sizeof(str1[0])-1;
  int size2=sizeof(str2)/sizeof(str2[0])-1;

  is_isomorphic(str1,str2,size1,size2);


  char str3[] = "omggod";
  char str4[] = "Freeim"; //o to i not valid

  //Get the size
  size1=sizeof(str3)/sizeof(str3[0])-1;
  size2=sizeof(str4)/sizeof(str4[0])-1;

  is_isomorphic(str3,str4,size1,size2);

  return 0;
}



Output

Codepaper : minkdidkt
Isomorphic
omggod : Freeim
Not Isomorphic
// Java program
// Check if two given strings are isomorphic

public class MyString {

  public void is_isomorphic(String str1, String str2) {

  boolean status = true;
  
  //Get the length of string
  int s1 = str1.length();
  int s2 = str2.length();

  if (s1 != s2) 
  {
    //When given string lengths are not equal
    status = false;
  }
  for (int i = 0; i < s1 && status == true; ++i) 
  {
    for (int j = i; j < s2 && status == true; ++j) 
    {
      //compare location [i] character are similar to another string or not
      if (str1.charAt(i) == str1.charAt(j) && str2.charAt(i) != str2.charAt(j)) 
      {
        status = false;
      }
    }
  }
  System.out.print(str1+" : "+str2+"\n" );
  if (status == true) 
  {
    System.out.print("Isomorphic\n");
  }
  else 
  {
    System.out.print("Not Isomorphic\n");
  }
}
  public static void main(String[] args) 
  {

    MyString obj = new MyString();
    
    //Test Case
    obj.is_isomorphic("Codepaper","minkdidkt");
    obj.is_isomorphic("omggod","Freeim");

  }
}

Output

Codepaper : minkdidkt
Isomorphic
omggod : Freeim
Not Isomorphic
// C++ program
// Check if two given strings are isomorphic
#include<iostream>

using namespace std;
class MyString {
	public:
		void is_isomorphic(string str1, string str2) {
			bool status = true;
			//Get the length of string
			int s1 = str1.size();
			int s2 = str2.size();
			if (s1 != s2) {
				//When given string lengths are not equal
				status = false;
			}
			for (int i = 0; i < s1 &&
				status == true; ++i) {
				for (int j = i; j < s2 &&
					status == true; ++j) {
					//compare location [i] character are similar to another string or not

					if (str1[i] == str1[j] &&
						str2[i] != str2[j]) {
						status = false;
					}
				}
			}
			cout << str1 << " : " << str2 << "\n";
			if (status == true) {
				cout << "Isomorphic\n";
			} else {
				cout << "Not Isomorphic\n";
			}
		}
};
int main() {
	MyString obj =  MyString();
	//Test Case
	obj.is_isomorphic("Codepaper", "minkdidkt");
	obj.is_isomorphic("omggod", "Freeim");
	return 0;
}

Output

Codepaper : minkdidkt
Isomorphic
omggod : Freeim
Not Isomorphic
// C# program
// Check if two given strings are isomorphic
using System;
public class MyString {
	public void is_isomorphic(String str1, String str2) {
		Boolean status = true;
		//Get the length of string
		int s1 = str1.Length;
		int s2 = str2.Length;
		if (s1 != s2) {
			//When given string lengths are not equal
			status = false;
		}
		for (int i = 0; i < s1 &&
			status == true; ++i) {
			for (int j = i; j < s2 &&
				status == true; ++j) {
				//compare location [i] character are similar to another string or not

				if (str1[i] == str1[j] &&
					str2[i] != str2[j]) {
					status = false;
				}
			}
		}
		Console.Write(str1 + " : " + str2 + "\n");
		if (status == true) {
			Console.Write("Isomorphic\n");
		} else {
			Console.Write("Not Isomorphic\n");
		}
	}
	public static void Main(String[] args) {
		MyString obj = new MyString();
		obj.is_isomorphic("Codepaper", "minkdidkt");
		obj.is_isomorphic("omggod", "Freeim");
	}
}

Output

Codepaper : minkdidkt
Isomorphic
omggod : Freeim
Not Isomorphic
<?php
// Php program
// Check if two given strings are isomorphic
class MyString {
	public 	function is_isomorphic($str1, $str2) {
		$status = true;
		//Get the length of string
		$s1 = strlen($str1);
		$s2 = strlen($str2);
		if ($s1 != $s2) {
			//When given string lengths are not equal
			$status = false;
		}
		for ($i = 0; $i < $s1 &&
			$status == true; ++$i) {
			for ($j = $i; $j < $s2 &&
				$status == true; ++$j) {
				//compare location [i] character are similar to another string or not

				if ($str1[$i] == $str1[$j] &&
					$str2[$i] != $str2[$j]) {
					$status = false;
				}
			}
		}
		echo($str1 ." : ". $str2 ."\n");
		if ($status == true) {
			echo("Isomorphic\n");
		} else {
			echo("Not Isomorphic\n");
		}
	}
}

function main() {
	$obj = new MyString();
	//Test Case
	$obj->is_isomorphic("Codepaper", "minkdidkt");
	$obj->is_isomorphic("omggod", "Freeim");

}
main();

Output

Codepaper : minkdidkt
Isomorphic
omggod : Freeim
Not Isomorphic
// Node Js program
// Check if two given strings are isomorphic
class MyString {
	is_isomorphic(str1, str2) {
		var status = true;
		//Get the length of string
		var s1 = str1.length;
		var s2 = str2.length;
		if (s1 != s2) {
			//When given string lengths are not equal
			status = false;
		}

		for (var i = 0; i < s1 &&
			status == true; ++i) {
			for (var j = i; j < s2 &&
				status == true; ++j) {
				//compare location [i] character are similar to another string or not

				if (str1[i] == str1[j] &&
					str2[i] != str2[j]) {
					status = false;
				}
			}
		}

		process.stdout.write(str1 + " : " + str2 + "\n");
		if (status == true) {
			process.stdout.write("Isomorphic\n");
		} else {
			process.stdout.write("Not Isomorphic\n");
		}
	}
}

function main(args) {
	var obj = new MyString();
	//Test Case
	obj.is_isomorphic("Codepaper", "minkdidkt");
	obj.is_isomorphic("omggod", "Freeim");
}

main();

Output

Codepaper : minkdidkt
Isomorphic
omggod : Freeim
Not Isomorphic
#  Python 3 program
#  Check if two given strings are isomorphic
class MyString :
	def is_isomorphic(self, str1, str2) :
		status = True
		# Get the length of string
		s1 = len(str1)
		s2 = len(str2)
		if (s1 != s2) :
			# When given string lengths are not equal
			status = False
		
		i = 0
		j = 0
		while (i < s1 and status == True) :
			j = i
			while (j < s2 and status == True) :
				# compare location [i] character are similar to another string or not

				if (str1[i] == str1[j] and str2[i] != str2[j]) :
					status = False
				
				j += 1
			
			i += 1
		
		print(str1 ," : ", str2 )
		if (status == True) :
			print("Isomorphic")
		else :
			print("Not Isomorphic")
		
	

def main() :
	obj = MyString()
	obj.is_isomorphic("Codepaper", "minkdidkt")
	obj.is_isomorphic("omggod", "Freeim")


if __name__ == "__main__":
	main()

Output

Codepaper  :  minkdidkt
Isomorphic
omggod  :  Freeim
Not Isomorphic
#  Ruby program
#  Check if two given strings are isomorphic
class MyString 
	def is_isomorphic(str1, str2) 
		status = true
		# Get the length of string
		s1 = str1.length()
		s2 = str2.length()
		if (s1 != s2) 
			# When given string lengths are not equal
			status = false
		end
		i = 0
		j = 0
		while (i < s1 &&
			status == true) 
			j = i
			while (j < s2 &&
				status == true) 
				# compare location [i] character are similar to another string or not
				if (str1[i] == str1[j] &&
					str2[i] != str2[j]) 
					status = false
				end
				j += 1
			end
			i += 1
		end
		print(str1 ," : ", str2 ,"\n")
		if (status == true) 
			print("Isomorphic\n")
		else 
			print("Not Isomorphic\n")
		end
	end
end
def main() 
	obj = MyString.new()
	obj.is_isomorphic("Codepaper", "minkdidkt")
	obj.is_isomorphic("omggod", "Freeim")
end
main()

Output

Codepaper : minkdidkt
Isomorphic
omggod : Freeim
Not Isomorphic
// Scala program
// Check if two given strings are isomorphic
class MyString {
	def is_isomorphic(str1: String, str2: String): Unit = {
		var status: Boolean = true;

		//Get the length of string
		var s1: Int = str1.length();
		var s2: Int = str2.length();

		if (s1 != s2) {
			//When given string lengths are not equal
			status = false;
		}
		var i: Int = 0;
		var j: Int = 0;
		while (i < s1 &&
			status == true) {
			j = i;
			while (j < s2 &&
				status == true) {
				//compare location [i] character are similar to another string or not

				if (str1(i) == str1(j) &&
					str2(i) != str2(j)) {
					status = false;
				}
				j += 1;
			}
			i += 1;
		}
		print(str1 + " : " + str2 + "\n");

		if (status == true) {
			print("Isomorphic\n");
		} else {
			print("Not Isomorphic\n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();
		obj.is_isomorphic("Codepaper", "minkdidkt");
		obj.is_isomorphic("omggod", "Freeim");
	}
}

Output

Codepaper : minkdidkt
Isomorphic
omggod : Freeim
Not Isomorphic
// Swift program
// Check if two given strings are isomorphic
class MyString {
	func is_isomorphic(_ text1: String, _ text2: String) {
		var status: Bool = true;
       	var str1 = Array(text1);
        var str2 = Array(text2);
		//Get the length of string
		let s1: Int = str1.count;
		let s2: Int = str2.count;
		if (s1 != s2) {
			//When given string lengths are not equal
			status = false;
		}
		var i: Int = 0;
		var j: Int = 0;
		while (i < s1 &&
			status == true) {
			j = i;
			while (j < s2 &&
				status == true) {
				//compare location [i] character are similar to another string or not

				if (str1[i] == str1[j] &&
					str2[i] != str2[j]) {
					status = false;
				}
				j += 1;
			}
			i += 1;
		}
		print(text1 ," : ", text2 ,"\n", terminator: "");
		if (status == true) {
			print("Isomorphic\n", terminator: "");
		} else {
			print("Not Isomorphic\n", terminator: "");
		}
	}
}
func main() {
	let obj: MyString = MyString();
	obj.is_isomorphic("Codepaper", "minkdidkt");
	obj.is_isomorphic("omggod", "Freeim");
}
main();

Output

Codepaper  :  minkdidkt
Isomorphic
omggod  :  Freeim
Not Isomorphic


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved