Check if two given strings are isomorphic to each other in C

Check if strings are isomorphic to Each Other

today we will be learning about how to Check if strings are isomorphic to Each Other using c programming language.

  • Input: str1 = “egg”, str2 = “add” 
  • Output: Yes 
Cpp program to check if two strings match where one string contains wildcard characters

Algorithm:

  • Initialize two arrays of size 256.
  • Iterate through characters of the given strings and increment the index equal to the ASCII value of the character at ith position.
  • If the are no conflicts in the mapping of the characters, print Yes. Otherwise, print No.
Check if strings are isomorphic

C program

Run

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

bool areIsomorphic (char *str1, char *str2) 
{

  // If the length of the strings
  // are not equal
  if (strlen (str1) != strlen (str2))
  {
    return false;
  }
  // Initialise two arrays
  int arr1[256] = { 0 }, arr2[256] = {0};
  
  // Traversing both the strings
  for (int i = 0; i < strlen (str1); i++)
  {

    // If current characters don't map
    if (arr1[(int) str1[i]] !=arr2[(int) str2[i]])
    {
      return false;
    }
    // Increment the count of characters
    // at their respective ASCII indices
    arr1[(int) str1[i]]++;
    arr2[(int) str2[i]]++;
  } 
  return true;
 }
// Driver Code
  int main ()  
  {
    char s1[] = "aab", s2[] = "xxy";
    if (areIsomorphic (s1, s2))
      printf ("Yes\n");
    else
      printf ("No\n");
  return 0;
  }