📜  比较两个字符串,只考虑字母数字字符

📅  最后修改于: 2022-05-13 01:57:07.663000             🧑  作者: Mango

比较两个字符串,只考虑字母数字字符

给定两个可以包含大小写字母、数字和特殊字符(如点、空格、逗号等)的字符串,如果它们是字母数字字符([ab]、[AB] 和 [0-9])相等或不相等。例如,字符串“Ram, Shyam”和“Ram-Shyam”都是相同的,“/.';[]”和“@# >”也是相同的。

例子:

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.

由于我们必须只比较字母数字字符,因此每当找到任何其他字符时,只需通过增加迭代器指针来忽略它。为此,只需使用两个整数变量 i 和 j 并将它们初始化为 0。现在运行一个循环来比较两个字符串的每个字符。如果字符是字母数字,则一一比较,否则将 i 或 j 的值加一。

下面是上述方法的实现:

C++
#include 
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 strings 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 strings 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


Python3
# 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 strings 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


Javascript


输出:
Equal
Unequal