Compare two strings considering only alphanumeric characters
Given two strings that can contain lower and uppercase alphabets, numbers and special characters like dots, blank spaces, commas, etc. Compare both strings considering only alphanumeric characters([a-b], [A-B] and [0-9]) if they are equal or not. For example, strings “Ram, Shyam” and “Ram-Shyam” both are the same and also “/.’;[]” and “@# >” are same.
Examples:
Input: str1 = "Ram, Shyam", str2 = " Ram - Shyam." Output: Equal Explanation: if we ignore all characters except alphanumeric characters then strings will be, str1 = "RamShyam" and str2 = "RamShyam". Therefore both strings are equal. Input : str1 = "aaa123", str2 = "@aaa-12-3" Output : Equal Input : str1 = "abc123", str2 = "123abc" Output : Unequal Explanation: In this, str1 = "abc123" and str2 = "123abc". Therefore both strings are not equal.
Since we have to compare only alphanumeric characters therefore whenever any other character is found simply ignore it by increasing iterator pointer. For doing it simply take two integer variables i and j and initialize them by 0. Now run a loop to compare each and every character of both strings. Compare one by one if the character is alphanumeric otherwise increase the value of i or j by one.
Below is the implementation of the above approach:
C++
#include <iostream> using namespace std; // Function to check alphanumeric equality of both strings bool CompareAlphanumeric(string& str1, string& str2) { // variable declaration int i, j; i = 0; j = 0; // Length of first string int len1 = str1.size(); // Length of second string int len2 = str2.size(); // To check each and every characters of both string while (i <= len1 && j <= len2) { // If the current character of the first string is not an // alphanumeric character, increase the pointer i while (i < len1 && (!((str1[i] >= 'a' && str1[i] <= 'z') || (str1[i] >= 'A' && str1[i] <= 'Z') || (str1[i] >= '0' && str1[i] <= '9')))) { i++; } // If the current character of the second string is not an // alphanumeric character, increase the pointer j while (j < len2 && (!((str2[j] >= 'a' && str2[j] <= 'z') || (str2[j] >= 'A' && str2[j] <= 'Z') || (str2[j] >= '0' && str2[j] <= '9')))) { j++; } // if all alphanumeric characters of both strings are same // then return true if (i == len1 && j == len2) return true; // if any alphanumeric characters of both strings are not same // then return false else if (str1[i] != str2[j]) return false; // If current character matched, // increase both pointers to check the next character else { i++; j++; } } // If not same, then return false return false; } // Function to print Equal or Unequal if strins are same or not void CompareAlphanumericUtil(string str1, string str2) { bool res; // check alphanumeric equality of both strings res = CompareAlphanumeric(str1, str2); // if both are alphanumeric equal, print Equal if (res == true) cout << "Equal" << endl; // otherwise print Unequal else cout << "Unequal" << endl; } // Driver code int main() { string str1, str2; str1 = "Ram, Shyam"; str2 = " Ram - Shyam."; CompareAlphanumericUtil(str1, str2); str1 = "abc123"; str2 = "123abc"; CompareAlphanumericUtil(str1, str2); return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to check alphanumeric // equality of both strings static boolean CompareAlphanumeric(char[] str1, char[] str2) { // variable declaration int i, j; i = 0; j = 0; // Length of first string int len1 = str1.length; // Length of second string int len2 = str2.length; // To check each and every characters // of both string while (i <= len1 && j <= len2) { // If the current character of the first string is not an // alphanumeric character, increase the pointer i while (i < len1 && (!((str1[i] >= 'a' && str1[i] <= 'z') || (str1[i] >= 'A' && str1[i] <= 'Z') || (str1[i] >= '0' && str1[i] <= '9')))) { i++; } // If the current character of the second string is not an // alphanumeric character, increase the pointer j while (j < len2 && (!((str2[j] >= 'a' && str2[j] <= 'z') || (str2[j] >= 'A' && str2[j] <= 'Z') || (str2[j] >= '0' && str2[j] <= '9')))) { j++; } // if all alphanumeric characters of // both strings are same then return true if (i == len1 && j == len2) { return true; } // if any alphanumeric characters of // both strings are not same then return false else if (str1[i] != str2[j]) { return false; } // If current character matched, // increase both pointers to // check the next character else { i++; j++; } } // If not same, then return false return false; } // Function to print Equal or Unequal // if strins are same or not static void CompareAlphanumericUtil(String str1, String str2) { boolean res; // check alphanumeric equality of both strings res = CompareAlphanumeric(str1.toCharArray(), str2.toCharArray()); // if both are alphanumeric equal, // print Equal if (res == true) { System.out.println("Equal"); } // otherwise print Unequal else { System.out.println("Unequal"); } } // Driver code public static void main(String[] args) { String str1, str2; str1 = "Ram, Shyam"; str2 = " Ram - Shyam."; CompareAlphanumericUtil(str1, str2); str1 = "abc123"; str2 = "123abc"; CompareAlphanumericUtil(str1, str2); } } // This code is contributed by Rajput-Ji |
Python 3
# Function to check alphanumeric equality # of both strings def CompareAlphanumeric(str1, str2): # variable declaration i = 0 j = 0 # Length of first string len1 = len(str1) # Length of second string len2 = len(str2) # To check each and every character of both string while (i <= len1 and j <= len2): # If the current character of the first string # is not an alphanumeric character, # increase the pointer i while (i < len1 and (((str1[i] >= 'a' and str1[i] <= 'z') or (str1[i] >= 'A' and str1[i] <= 'Z') or (str1[i] >= '0' and str1[i] <= '9')) == False)): i += 1 # If the current character of the second string # is not an alphanumeric character, # increase the pointer j while (j < len2 and (((str2[j] >= 'a' and str2[j] <= 'z') or (str2[j] >= 'A' and str2[j] <= 'Z') or (str2[j] >= '0' and str2[j] <= '9')) == False)): j += 1 # if all alphanumeric characters of # both strings are same, then return true if (i == len1 and j == len2): return True # if any alphanumeric characters of # both strings are not same, then return false elif (str1[i] != str2[j]): return False # If current character matched, # increase both pointers # to check the next character else: i += 1 j += 1 # If not same, then return false return False # Function to print Equal or Unequal # if strings are same or not def CompareAlphanumericUtil(str1, str2): # check alphanumeric equality of both strings res = CompareAlphanumeric(str1, str2) # if both are alphanumeric equal, print Equal if (res == True): print("Equal") # otherwise print Unequal else: print("Unequal") # Driver code if __name__ == '__main__': str1 = "Ram, Shyam" str2 = " Ram - Shyam." CompareAlphanumericUtil(str1, str2) str1 = "abc123" str2 = "123abc" CompareAlphanumericUtil(str1, str2) # This code is contributed by Surendra_Gangwar |
C#
// C# implementation of the approach using System; class GFG { // Function to check alphanumeric // equality of both strings static bool CompareAlphanumeric(char []str1, char []str2) { // variable declaration int i, j; i = 0; j = 0; // Length of first string int len1 = str1.Length; // Length of second string int len2 = str2.Length; // To check each and every characters // of both string while (i <= len1 && j <= len2) { // If the current character of the first // string is not an alphanumeric character, // increase the pointer i while (i < len1 && (!((str1[i] >= 'a' && str1[i] <= 'z') || (str1[i] >= 'A' && str1[i] <= 'Z') || (str1[i] >= '0' && str1[i] <= '9')))) { i++; } // If the current character of the second // string is not an alphanumeric character, // increase the pointer j while (j < len2 && (!((str2[j] >= 'a' && str2[j] <= 'z') || (str2[j] >= 'A' && str2[j] <= 'Z') || (str2[j] >= '0' && str2[j] <= '9')))) { j++; } // if all alphanumeric characters of // both strings are same then return true if (i == len1 && j == len2) { return true; } // if any alphanumeric characters of // both strings are not same then return false else if (str1[i] != str2[j]) { return false; } // If current character matched, // increase both pointers to // check the next character else { i++; j++; } } // If not same, then return false return false; } // Function to print Equal or Unequal // if strins are same or not static void CompareAlphanumericUtil(string str1, string str2) { bool res; // check alphanumeric equality of both strings res = CompareAlphanumeric(str1.ToCharArray(), str2.ToCharArray()); // if both are alphanumeric equal, // print Equal if (res == true) { Console.WriteLine("Equal"); } // otherwise print Unequal else { Console.WriteLine("Unequal"); } } // Driver code public static void Main() { string str1, str2; str1 = "Ram, Shyam"; str2 = " Ram - Shyam."; CompareAlphanumericUtil(str1, str2); str1 = "abc123"; str2 = "123abc"; CompareAlphanumericUtil(str1, str2); } } // This code is contributed by AnkitRai01 |
Equal Unequal
Recommended Posts:
- Remove characters from the first string which are present in the second string
- A Program to check if strings are rotations of each other or not
- Print list items containing all characters of a given word
- Find the smallest window in a string containing all characters of another string
- Length of the longest substring without repeating characters
- Check whether two strings are anagram of each other
- Print all permutations with repetition of characters
- Print all interleavings of given two strings
- Interleaving of two given strings with no common characters
- String matching where one string contains wildcard characters
- Find if a string is interleaved of two other strings | DP-33
- Find the first non-repeating character from a stream of characters
- Rearrange a string so that all same characters become d distance away
- Given a sorted dictionary of an alien language, find order of characters
- Find if an array of strings can be chained to form a circle | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


