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++

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

Equal
Unequal


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : Rajput-Ji



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.