📌  相关文章
📜  检查两个字符串是否相同,忽略它们的大小写

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

检查两个字符串是否相同,忽略它们的大小写

给定两个字符串str1 和 str2。任务是检查两个给定字符串是否相同,如果遵循不区分大小写的比较,即在Java中忽略字符串的大小写。
例子:

Input: str1 = "Geeks", str2 = "geeks"
Output: Same

Input: str1 = "Geek", str2 = "geeksforgeeks"
Output: Not Same

方法1:朴素的方法

  • 将第一个字符串的每个字符与第二个字符串的对应字符进行比较。
  • 如果匹配,则比较下一个字符。
  • 如果不匹配,则忽略它们的大小写来检查它是否匹配。
  • 如果匹配,则比较下一个字符。
  • 如果所有字符都匹配,则返回 true
  • 如果任何字符不匹配,则返回 false。

下面给出实现。

C++
#include 
using namespace std;
 
// Function to compare two strings
// ignoring their cases
bool equalIgnoreCase(string str1, string str2)
{
    int i = 0;
 
    // length of first string
    int len1 = str1.size();
 
    // length of second string
    int len2 = str2.size();
 
    // if length is not same
    // simply return false since both string
    // can not be same if length is not equal
    if (len1 != len2)
        return false;
 
    // loop to match one by one
    // all characters of both string
    while (i < len1) {
 
        // if current characters of both string are same,
        // increase value of i to compare next character
        if (str1[i] == str2[i]) {
            i++;
        }
 
        // if any character of first string
        // is some special character
        // or numeric character and
        // not same as corresponding character
        // of second string then return false
        else if (!((str1[i] >= 'a' && str1[i] <= 'z')
                   || (str1[i] >= 'A' && str1[i] <= 'Z'))) {
            return false;
        }
 
        // do the same for second string
        else if (!((str2[i] >= 'a' && str2[i] <= 'z')
                   || (str2[i] >= 'A' && str2[i] <= 'Z'))) {
            return false;
        }
 
        // this block of code will be executed
        // if characters of both strings
        // are of different cases
        else {
            // compare characters by ASCII value
            if (str1[i] >= 'a' && str1[i] <= 'z') {
                if (str1[i] - 32 != str2[i])
                    return false;
            }
 
            else if (str1[i] >= 'A' && str1[i] <= 'Z') {
                if (str1[i] + 32 != str2[i])
                    return false;
            }
 
            // if characters matched,
            // increase the value of i to compare next char
            i++;
 
        } // end of outer else block
 
    } // end of while loop
 
    // if all characters of the first string
    // are matched with corresponding
    // characters of the second string,
    // then return true
    return true;
 
} // end of equalIgnoreCase function
 
// Function to print the same or not same
// if strings are equal or not equal
void equalIgnoreCaseUtil(string str1, string str2)
{
    bool res = equalIgnoreCase(str1, str2);
 
    if (res == true)
        cout << "Same" << endl;
 
    else
        cout << "Not Same" << endl;
}
 
// Driver Code
int main()
{
    string str1, str2;
 
    str1 = "Geeks";
    str2 = "geeks";
    equalIgnoreCaseUtil(str1, str2);
 
    str1 = "Geek";
    str2 = "geeksforgeeks";
    equalIgnoreCaseUtil(str1, str2);
    return 0;
}


Java
class GFG
{
// Function to compare two strings
// ignoring their cases
static boolean equalIgnoreCase(String str1, String str2)
{
    int i = 0;
 
    // length of first string
    int len1 = str1.length();
 
    // length of second string
    int len2 = str2.length();
 
    // if length is not same
    // simply return false since both string
    // can not be same if length is not equal
    if (len1 != len2)
        return false;
 
    // loop to match one by one
    // all characters of both string
    while (i < len1)
    {
 
        // if current characters of both string are same,
        // increase value of i to compare next character
        if (str1.charAt(i) == str2.charAt(i))
        {
            i++;
        }
 
        // if any character of first string
        // is some special character
        // or numeric character and
        // not same as corresponding character
        // of second string then return false
        else if (!((str1.charAt(i) >= 'a' && str1.charAt(i) <= 'z')
                || (str1.charAt(i) >= 'A' && str1.charAt(i) <= 'Z')))
        {
            return false;
        }
 
        // do the same for second string
        else if (!((str2.charAt(i) >= 'a' && str2.charAt(i) <= 'z')
                || (str2.charAt(i) >= 'A' && str2.charAt(i) <= 'Z')))
        {
            return false;
        }
 
        // this block of code will be executed
        // if characters of both strings
        // are of different cases
        else
        {
            // compare characters by ASCII value
            if (str1.charAt(i) >= 'a' && str1.charAt(i) <= 'z')
            {
                if (str1.charAt(i) - 32 != str2.charAt(i))
                    return false;
            }
 
            else if (str1.charAt(i) >= 'A' && str1.charAt(i) <= 'Z')
            {
                if (str1.charAt(i) + 32 != str2.charAt(i))
                    return false;
            }
 
            // if characters matched,
            // increase the value of i to compare next char
            i++;
 
        } // end of outer else block
 
    } // end of while loop
 
    // if all characters of the first string
    // are matched with corresponding
    // characters of the second string,
    // then return true
    return true;
 
} // end of equalIgnoreCase function
 
// Function to print the same or not same
// if strings are equal or not equal
static void equalIgnoreCaseUtil(String str1, String str2)
{
    boolean res = equalIgnoreCase(str1, str2);
 
    if (res == true)
        System.out.println( "Same" );
 
    else
        System.out.println( "Not Same" );
}
 
// Driver Code
public static void main(String args[])
{
    String str1, str2;
 
    str1 = "Geeks";
    str2 = "geeks";
    equalIgnoreCaseUtil(str1, str2);
 
    str1 = "Geek";
    str2 = "geeksforgeeks";
    equalIgnoreCaseUtil(str1, str2);
}
}
 
// This code is contributed by Arnab Kundu


Python3
# Function to compare two strings
# ignoring their cases
def equalIgnoreCase(str1, str2):
    i = 0
 
    # length of first string
    len1 = len(str1)
 
    # length of second string
    len2 = len(str2)
 
    # if length is not same
    # simply return false since both string
    # can not be same if length is not equal
    if (len1 != len2):
        return False
 
    # loop to match one by one
    # all characters of both string
    while (i < len1):
         
        # if current characters of both string are same,
        # increase value of i to compare next character
        if (str1[i] == str2[i]):
            i += 1
 
        # if any character of first string
        # is some special character
        # or numeric character and
        # not same as corresponding character
        # of second string then return false
        elif (((str1[i] >= 'a' and str1[i] <= 'z') or
               (str1[i] >= 'A' and str1[i] <= 'Z')) == False):
            return False
 
        # do the same for second string
        elif (((str2[i] >= 'a' and str2[i] <= 'z') or
               (str2[i] >= 'A' and str2[i] <= 'Z')) == False):
            return False
 
        # this block of code will be executed
        # if characters of both strings
        # are of different cases
        else:
             
            # compare characters by ASCII value
            if (str1[i] >= 'a' and str1[i] <= 'z'):
                if (ord(str1[i]) - 32 != ord(str2[i])):
                    return False
 
            elif (str1[i] >= 'A' and str1[i] <= 'Z'):
                if (ord(str1[i]) + 32 != ord(str2[i])):
                    return False
 
            # if characters matched,
            # increase the value of i
            # to compare next char
            i += 1
 
        # end of outer else block
 
    # end of while loop
 
    # if all characters of the first string
    # are matched with corresponding
    # characters of the second string,
    # then return true
    return True
 
# end of equalIgnoreCase function
 
# Function to print the same or not same
# if strings are equal or not equal
def equalIgnoreCaseUtil(str1, str2):
    res = equalIgnoreCase(str1, str2)
 
    if (res == True):
        print("Same")
 
    else:
        print("Not Same")
 
# Driver Code
if __name__ == '__main__':
    str1 = "Geeks"
    str2 = "geeks"
    equalIgnoreCaseUtil(str1, str2)
 
    str1 = "Geek"
    str2 = "geeksforgeeks"
    equalIgnoreCaseUtil(str1, str2)
 
# This code is contributed by
# Surendra_Gangwar


C#
using System;
class GFG
{
// Function to compare two strings
// ignoring their cases
static bool equalIgnoreCase(string str1, string str2)
{
    int i = 0;
 
    // length of first string
    int len1 = str1.Length;
 
    // length of second string
    int len2 = str2.Length;
 
    // if length is not same
    // simply return false since both string
    // can not be same if length is not equal
    if (len1 != len2)
        return false;
 
    // loop to match one by one
    // all characters of both string
    while (i < len1)
    {
 
        // if current characters of both string are same,
        // increase value of i to compare next character
        if (str1[i] == str2[i])
        {
            i++;
        }
 
        // if any character of first string
        // is some special character
        // or numeric character and
        // not same as corresponding character
        // of second string then return false
        else if (!((str1[i] >= 'a' && str1[i] <= 'z')
                || (str1[i] >= 'A' && str1[i] <= 'Z')))
        {
            return false;
        }
 
        // do the same for second string
        else if (!((str2[i] >= 'a' && str2[i] <= 'z')
                || (str2[i] >= 'A' && str2[i] <= 'Z')))
        {
            return false;
        }
 
        // this block of code will be executed
        // if characters of both strings
        // are of different cases
        else
        {
            // compare characters by ASCII value
            if (str1[i] >= 'a' && str1[i] <= 'z')
            {
                if (str1[i] - 32 != str2[i])
                    return false;
            }
 
            else if (str1[i] >= 'A' && str1[i] <= 'Z')
            {
                if (str1[i] + 32 != str2[i])
                    return false;
            }
 
            // if characters matched,
            // increase the value of i to compare next char
            i++;
 
        } // end of outer else block
 
    } // end of while loop
 
    // if all characters of the first string
    // are matched with corresponding
    // characters of the second string,
    // then return true
    return true;
 
} // end of equalIgnoreCase function
 
// Function to print the same or not same
// if strings are equal or not equal
static void equalIgnoreCaseUtil(string str1, string str2)
{
    bool res = equalIgnoreCase(str1, str2);
 
    if (res == true)
        Console.WriteLine( "Same" );
 
    else
        Console.WriteLine( "Not Same" );
}
 
// Driver Code
public static void Main()
{
    string str1, str2;
 
    str1 = "Geeks";
    str2 = "geeks";
    equalIgnoreCaseUtil(str1, str2);
 
    str1 = "Geek";
    str2 = "geeksforgeeks";
    equalIgnoreCaseUtil(str1, str2);
}
}
 
// This code is contributed by ihritik


Javascript


C++
#include
using namespace std;
 
// Function to compare two strings ignoring their cases
bool equalIgnoreCase(string str1, string str2)
{
    int i = 0;
 
    // Convert to uppercase
    // using transform() function and ::toupper in STL
    transform(str1.begin(), str1.end(), str1.begin(), ::toupper);
    transform(str2.begin(), str2.end(), str2.begin(), ::toupper);
 
    // Comparing both using inbuilt function
    int x = str1.compare(str2);
 
    // if strings are equal,
    // return true otherwise false
    if (x != 0)
        return false;
    else
        return true;
}
 
// Function to print the same or not same
// if strings are equal or not equal
void equalIgnoreCaseUtil(string str1, string str2)
{
    bool res = equalIgnoreCase(str1, str2);
 
    if (res == true)
        cout << "Same" << endl;
 
    else
        cout << "Not Same" << endl;
}
 
// Driver Code
int main()
{
    string str1, str2;
 
    str1 = "Geeks";
    str2 = "geeks";
    equalIgnoreCaseUtil(str1, str2);
 
    str1 = "Geek";
    str2 = "geeksforgeeks";
    equalIgnoreCaseUtil(str1, str2);
    return 0;
}


Java
class GFG
{
    // Function to compare two Strings ignoring their cases
    static boolean equalIgnoreCase(String str1, String str2)
    {
        int i = 0;
 
        // Convert to lowercase
        // using toUpperCase function
        str1 = str1.toUpperCase();
        str2 = str2.toUpperCase();
 
        // Comparing both using inbuilt function
        int x = str1.compareTo(str2);
 
        // if Strings are equal,
        // return true otherwise false
        if (x != 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
 
    // Function to print the same or not same
    // if Strings are equal or not equal
    static void equalIgnoreCaseUtil(String str1, String str2)
    {
        boolean res = equalIgnoreCase(str1, str2);
 
        if (res == true)
        {
            System.out.println("Same");
        }
        else
        {
            System.out.println("Not Same");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String str1, str2;
 
        str1 = "Geeks";
        str2 = "geeks";
        equalIgnoreCaseUtil(str1, str2);
 
        str1 = "Geek";
        str2 = "geeksforgeeks";
        equalIgnoreCaseUtil(str1, str2);
    }
}
 
// This code has been contributed by 29AjayKumar


Python3
# Python 3 Code for above approach
 
# Function to compare two strings
# ignoring their cases
def equalIgnoreCase(str1, str2) :
 
    # Convert to uppercase
    str1 = str1.upper();
    str2 = str2.upper();
 
    # if strings are equal,
    # return true otherwise false
    x = str1 == str2;
     
    return x;
 
# Function to print the same or not same
# if strings are equal or not equal
def equalIgnoreCaseUtil(str1, str2) :
 
    res = equalIgnoreCase(str1, str2);
 
    if (res == True) :
        print("Same");
    else :
        print("Not Same");
 
# Driver Code
if __name__ == "__main__" :
 
    str1 = "Geeks";
    str2 = "geeks";
    equalIgnoreCaseUtil(str1, str2);
 
    str1 = "Geek";
    str2 = "geeksforgeeks";
    equalIgnoreCaseUtil(str1, str2);
 
# This code is contributed by Ryuga


C#
using System;
public class GFG
{
    // Function to compare two Strings ignoring their cases
    static bool equalIgnoreCase(String str1, String str2)
    {
 
        // Convert to lowercase
        // using toUpperCase function
        str1 = str1.ToUpper();
        str2 = str2.ToUpper();
 
        // Comparing both using inbuilt function
        int x = str1.CompareTo(str2);
 
        // if Strings are equal,
        // return true otherwise false
        if (x != 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
 
    // Function to print the same or not same
    // if Strings are equal or not equal
    static void equalIgnoreCaseUtil(String str1, String str2)
    {
        bool res = equalIgnoreCase(str1, str2);
 
        if (res == true)
        {
            Console.WriteLine("Same");
        }
        else
        {
            Console.WriteLine("Not Same");
        }
    }
 
    // Driver Code
    public static void Main()
    {
        String str1, str2;
 
        str1 = "Geeks";
        str2 = "geeks";
        equalIgnoreCaseUtil(str1, str2);
 
        str1 = "Geek";
        str2 = "geeksforgeeks";
        equalIgnoreCaseUtil(str1, str2);
    }
}
 
/* This code contributed by PrinciRaj1992 */


Javascript


C++
#include
using namespace std;
 
// Function to compare two strings ignoring their cases
bool equalIgnoreCase(string str1, string str2)
{
    int i = 0;
 
    // Convert to lowercase
    // using transform() function and ::tolower in STL
    transform(str1.begin(), str1.end(), str1.begin(), ::tolower);
    transform(str2.begin(), str2.end(), str2.begin(), ::tolower);
 
    // Comparing both using inbuilt function
    int x = str1.compare(str2);
 
    // if strings are equal,
    // return true otherwise false
    if (x != 0)
        return false;
    else
        return true;
}
 
// Function to print the same or not same
// if strings are equal or not equal
void equalIgnoreCaseUtil(string str1, string str2)
{
    bool res = equalIgnoreCase(str1, str2);
 
    if (res == true)
        cout << "Same" << endl;
 
    else
        cout << "Not Same" << endl;
}
 
// Driver Code
int main()
{
    string str1, str2;
 
    str1 = "Geeks";
    str2 = "geeks";
    equalIgnoreCaseUtil(str1, str2);
 
    str1 = "Geek";
    str2 = "geeksforgeeks";
    equalIgnoreCaseUtil(str1, str2);
    return 0;
}


Java
// A Java Program to find the longest common prefix
class GFG
{
 
// Function to compare two strings ignoring their cases
static boolean equalIgnoreCase(String str1, String str2)
{
    int i = 0;
 
    // Convert to lowercase
    // using toLowerCase function
    str1 = str1.toLowerCase();
    str2 = str2.toLowerCase();
 
    // Comparing both using inbuilt function
    int x = str1.compareTo(str2);
 
    // if strings are equal,
    // return true otherwise false
    return x == 0;
}
 
// Function to print the same or not same
// if strings are equal or not equal
static void equalIgnoreCaseUtil(String str1, String str2)
{
    boolean res = equalIgnoreCase(str1, str2);
 
    if (res == true)
        System.out.println("Same");
 
    else
        System.out.println("Not Same");
}
 
// Driver Code
public static void main(String[] args)
{
    String str1, str2;
 
    str1 = "Geeks";
    str2 = "geeks";
    equalIgnoreCaseUtil(str1, str2);
 
    str1 = "Geek";
    str2 = "geeksforgeeks";
    equalIgnoreCaseUtil(str1, str2);
}
}
 
// This code contributed by Rajput-Ji


Python3
# Python 3 Code for above approach
 
# Function to compare two strings
# ignoring their cases
def equalIgnoreCase(str1, str2) :
 
    # Convert to lower case
    str1 = str1.lower();
    str2 = str2.lower();
 
    # if strings are equal,
    # return true otherwise false
    x = str1 == str2;
     
    return x;
 
# Function to print the same or not same
# if strings are equal or not equal
def equalIgnoreCaseUtil(str1, str2) :
 
    res = equalIgnoreCase(str1, str2);
 
    if (res == True) :
        print("Same");
    else :
        print("Not Same");
 
# Driver Code
if __name__ == "__main__" :
 
    str1 = "Geeks";
    str2 = "geeks";
    equalIgnoreCaseUtil(str1, str2);
 
    str1 = "Geek";
    str2 = "geeksforgeeks";
    equalIgnoreCaseUtil(str1, str2);
 
# This code is contributed by ihritik


C#
using System;
 
class GFG
{
    // Function to compare two Strings ignoring their cases
    static bool equalIgnoreCase(String str1, String str2)
    {
 
        // Convert to lowercase
        // using toUpperCase function
        str1 = str1.ToUpper();
        str2 = str2.ToUpper();
 
        // Comparing both using inbuilt function
        int x = str1.CompareTo(str2);
 
        // if Strings are equal,
        // return true otherwise false
        if (x != 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
 
    // Function to print the same or not same
    // if Strings are equal or not equal
    static void equalIgnoreCaseUtil(String str1, String str2)
    {
        bool res = equalIgnoreCase(str1, str2);
 
        if (res == true)
        {
            Console.WriteLine("Same");
        }
        else
        {
            Console.WriteLine("Not Same");
        }
    }
 
    // Driver Code
    public static void Main()
    {
        String str1, str2;
 
        str1 = "Geeks";
        str2 = "geeks";
        equalIgnoreCaseUtil(str1, str2);
 
        str1 = "Geek";
        str2 = "geeksforgeeks";
        equalIgnoreCaseUtil(str1, str2);
    }
}
 
/* This code contributed by PrinciRaj1992 */


Javascript


输出:
Same
Not Same

方法二:使用 toUpperCase()函数

  1. 将两个字符串的所有小写字符都更改为大写并进行比较。
  2. 如果它们相等,则打印“same”,否则打印“Not Same”。

下面给出实现。

C++

#include
using namespace std;
 
// Function to compare two strings ignoring their cases
bool equalIgnoreCase(string str1, string str2)
{
    int i = 0;
 
    // Convert to uppercase
    // using transform() function and ::toupper in STL
    transform(str1.begin(), str1.end(), str1.begin(), ::toupper);
    transform(str2.begin(), str2.end(), str2.begin(), ::toupper);
 
    // Comparing both using inbuilt function
    int x = str1.compare(str2);
 
    // if strings are equal,
    // return true otherwise false
    if (x != 0)
        return false;
    else
        return true;
}
 
// Function to print the same or not same
// if strings are equal or not equal
void equalIgnoreCaseUtil(string str1, string str2)
{
    bool res = equalIgnoreCase(str1, str2);
 
    if (res == true)
        cout << "Same" << endl;
 
    else
        cout << "Not Same" << endl;
}
 
// Driver Code
int main()
{
    string str1, str2;
 
    str1 = "Geeks";
    str2 = "geeks";
    equalIgnoreCaseUtil(str1, str2);
 
    str1 = "Geek";
    str2 = "geeksforgeeks";
    equalIgnoreCaseUtil(str1, str2);
    return 0;
}

Java

class GFG
{
    // Function to compare two Strings ignoring their cases
    static boolean equalIgnoreCase(String str1, String str2)
    {
        int i = 0;
 
        // Convert to lowercase
        // using toUpperCase function
        str1 = str1.toUpperCase();
        str2 = str2.toUpperCase();
 
        // Comparing both using inbuilt function
        int x = str1.compareTo(str2);
 
        // if Strings are equal,
        // return true otherwise false
        if (x != 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
 
    // Function to print the same or not same
    // if Strings are equal or not equal
    static void equalIgnoreCaseUtil(String str1, String str2)
    {
        boolean res = equalIgnoreCase(str1, str2);
 
        if (res == true)
        {
            System.out.println("Same");
        }
        else
        {
            System.out.println("Not Same");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String str1, str2;
 
        str1 = "Geeks";
        str2 = "geeks";
        equalIgnoreCaseUtil(str1, str2);
 
        str1 = "Geek";
        str2 = "geeksforgeeks";
        equalIgnoreCaseUtil(str1, str2);
    }
}
 
// This code has been contributed by 29AjayKumar

Python3

# Python 3 Code for above approach
 
# Function to compare two strings
# ignoring their cases
def equalIgnoreCase(str1, str2) :
 
    # Convert to uppercase
    str1 = str1.upper();
    str2 = str2.upper();
 
    # if strings are equal,
    # return true otherwise false
    x = str1 == str2;
     
    return x;
 
# Function to print the same or not same
# if strings are equal or not equal
def equalIgnoreCaseUtil(str1, str2) :
 
    res = equalIgnoreCase(str1, str2);
 
    if (res == True) :
        print("Same");
    else :
        print("Not Same");
 
# Driver Code
if __name__ == "__main__" :
 
    str1 = "Geeks";
    str2 = "geeks";
    equalIgnoreCaseUtil(str1, str2);
 
    str1 = "Geek";
    str2 = "geeksforgeeks";
    equalIgnoreCaseUtil(str1, str2);
 
# This code is contributed by Ryuga

C#

using System;
public class GFG
{
    // Function to compare two Strings ignoring their cases
    static bool equalIgnoreCase(String str1, String str2)
    {
 
        // Convert to lowercase
        // using toUpperCase function
        str1 = str1.ToUpper();
        str2 = str2.ToUpper();
 
        // Comparing both using inbuilt function
        int x = str1.CompareTo(str2);
 
        // if Strings are equal,
        // return true otherwise false
        if (x != 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
 
    // Function to print the same or not same
    // if Strings are equal or not equal
    static void equalIgnoreCaseUtil(String str1, String str2)
    {
        bool res = equalIgnoreCase(str1, str2);
 
        if (res == true)
        {
            Console.WriteLine("Same");
        }
        else
        {
            Console.WriteLine("Not Same");
        }
    }
 
    // Driver Code
    public static void Main()
    {
        String str1, str2;
 
        str1 = "Geeks";
        str2 = "geeks";
        equalIgnoreCaseUtil(str1, str2);
 
        str1 = "Geek";
        str2 = "geeksforgeeks";
        equalIgnoreCaseUtil(str1, str2);
    }
}
 
/* This code contributed by PrinciRaj1992 */

Javascript


输出:
Same
Not Same

方法三:使用 toLowerCase()函数

  1. 将两个字符串的所有字符都更改为小写并进行比较。
  2. 如果它们相等,则打印“same”,否则打印“Not Same”。

下面给出实现。

C++

#include
using namespace std;
 
// Function to compare two strings ignoring their cases
bool equalIgnoreCase(string str1, string str2)
{
    int i = 0;
 
    // Convert to lowercase
    // using transform() function and ::tolower in STL
    transform(str1.begin(), str1.end(), str1.begin(), ::tolower);
    transform(str2.begin(), str2.end(), str2.begin(), ::tolower);
 
    // Comparing both using inbuilt function
    int x = str1.compare(str2);
 
    // if strings are equal,
    // return true otherwise false
    if (x != 0)
        return false;
    else
        return true;
}
 
// Function to print the same or not same
// if strings are equal or not equal
void equalIgnoreCaseUtil(string str1, string str2)
{
    bool res = equalIgnoreCase(str1, str2);
 
    if (res == true)
        cout << "Same" << endl;
 
    else
        cout << "Not Same" << endl;
}
 
// Driver Code
int main()
{
    string str1, str2;
 
    str1 = "Geeks";
    str2 = "geeks";
    equalIgnoreCaseUtil(str1, str2);
 
    str1 = "Geek";
    str2 = "geeksforgeeks";
    equalIgnoreCaseUtil(str1, str2);
    return 0;
}

Java

// A Java Program to find the longest common prefix
class GFG
{
 
// Function to compare two strings ignoring their cases
static boolean equalIgnoreCase(String str1, String str2)
{
    int i = 0;
 
    // Convert to lowercase
    // using toLowerCase function
    str1 = str1.toLowerCase();
    str2 = str2.toLowerCase();
 
    // Comparing both using inbuilt function
    int x = str1.compareTo(str2);
 
    // if strings are equal,
    // return true otherwise false
    return x == 0;
}
 
// Function to print the same or not same
// if strings are equal or not equal
static void equalIgnoreCaseUtil(String str1, String str2)
{
    boolean res = equalIgnoreCase(str1, str2);
 
    if (res == true)
        System.out.println("Same");
 
    else
        System.out.println("Not Same");
}
 
// Driver Code
public static void main(String[] args)
{
    String str1, str2;
 
    str1 = "Geeks";
    str2 = "geeks";
    equalIgnoreCaseUtil(str1, str2);
 
    str1 = "Geek";
    str2 = "geeksforgeeks";
    equalIgnoreCaseUtil(str1, str2);
}
}
 
// This code contributed by Rajput-Ji

Python3

# Python 3 Code for above approach
 
# Function to compare two strings
# ignoring their cases
def equalIgnoreCase(str1, str2) :
 
    # Convert to lower case
    str1 = str1.lower();
    str2 = str2.lower();
 
    # if strings are equal,
    # return true otherwise false
    x = str1 == str2;
     
    return x;
 
# Function to print the same or not same
# if strings are equal or not equal
def equalIgnoreCaseUtil(str1, str2) :
 
    res = equalIgnoreCase(str1, str2);
 
    if (res == True) :
        print("Same");
    else :
        print("Not Same");
 
# Driver Code
if __name__ == "__main__" :
 
    str1 = "Geeks";
    str2 = "geeks";
    equalIgnoreCaseUtil(str1, str2);
 
    str1 = "Geek";
    str2 = "geeksforgeeks";
    equalIgnoreCaseUtil(str1, str2);
 
# This code is contributed by ihritik

C#

using System;
 
class GFG
{
    // Function to compare two Strings ignoring their cases
    static bool equalIgnoreCase(String str1, String str2)
    {
 
        // Convert to lowercase
        // using toUpperCase function
        str1 = str1.ToUpper();
        str2 = str2.ToUpper();
 
        // Comparing both using inbuilt function
        int x = str1.CompareTo(str2);
 
        // if Strings are equal,
        // return true otherwise false
        if (x != 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
 
    // Function to print the same or not same
    // if Strings are equal or not equal
    static void equalIgnoreCaseUtil(String str1, String str2)
    {
        bool res = equalIgnoreCase(str1, str2);
 
        if (res == true)
        {
            Console.WriteLine("Same");
        }
        else
        {
            Console.WriteLine("Not Same");
        }
    }
 
    // Driver Code
    public static void Main()
    {
        String str1, str2;
 
        str1 = "Geeks";
        str2 = "geeks";
        equalIgnoreCaseUtil(str1, str2);
 
        str1 = "Geek";
        str2 = "geeksforgeeks";
        equalIgnoreCaseUtil(str1, str2);
    }
}
 
/* This code contributed by PrinciRaj1992 */

Javascript


输出:
Same
Not Same