Horje
Compare Strings for equality or lexicographical order in different programming Languages

Given two strings string1 and string2, the task is to check if these two strings are equal or not. 

Examples:

Input: string1 = “GeeksforGeeks”, string2 = “GeeksforGeeks” 
Output: Yes 

Input: string1 = “Geeks for Geeks”, string2 = “Geeks for Geeks” 
Output: Yes 

Input: string1 = “GeeksforGeeks”, string2 = “Geeks” 
Output: No 

Input: string1 = “Geeks for Geeks”, string2 = “Geeks for geeks” 
Output: No

Approach 1: Using Relational Operator

Programming Language

How to check for String Equality

C

strcmp()

C++

==

Java

equals()

Python

is

C# ==
JavaScript ===

C++

// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;
 
// Drivers code
int main()
{
 
    // First string.
    string s1 = "GeeksforGeeks";
 
    // Second string.
    string s2 = "Geeks for geeks";
 
    // Check condition.
    if (s1 == s2) {
        cout << "Strings Are Equal" << endl;
    }
    else {
        cout << "Strings Are Not Equal" << endl;
    }
    return 0;
}

Java

import java.util.*;
 
public class Main {
    public static void main(String[] args)
    {
        // first string
        String s1 = "GeeksforGeeks";
        // second string
        String s2 = "Geeks for geeks";
        // check condition
        if (s1.equals(s2)) {
            System.out.println("Strings Are Equal");
        }
        else {
            System.out.println("Strings Are Not Equal");
        }
    }
}

Python

# Python3 program to check 2 strings are identical or not
 
if __name__ == "__main__":
 
    # first string.
string1 = "GeeksforGeeks"
# second string.
string2 = "Geeks for geeks"
 
# check condition
if (string1 is string2):
    print("Strings Are Equal")
else:
    print("Strings Are Not Equal")

C#

using System;
 
class GFG {
    static void Main(string[] args)
    {
        // first string
        string s1 = "GeeksforGeeks";
        // second string
        string s2 = "Geeks for geeks";
        // check condition
        if (s1 == s2) {
            Console.WriteLine("Strings Are Equal");
        }
        else {
            Console.WriteLine("Strings Are Not Equal");
        }
    }
}

Javascript

// js code implementation
 
const s1 = "GeeksforGeeks";
const s2 = "Geeks for geeks";
 
if (s1 === s2) {
    console.log("Strings Are Equal");
} else {
    console.log("Strings Are Not Equal");
}

Output

Strings Are Not Equal










Approach 2: Using Comparator

Define a function to compare values with the following conditions:

  • Given strings are equal if:
    • both the strings are equal lexicographically, i.e.(string1 == string2) it returns 0.
  • Given strings are not equal if :
    • (string1 > string2) it returns a positive value.
    • (string1 < string2) it returns a negative value.

The value is calculated as (int)str1.charAt(i) – (int)str2.charAt(i)

Below is the implementation for the above approach:

C++

// C++ program to Compare two strings
// lexicographically
#include <bits/stdc++.h>
using namespace std;
 
// This function compares two strings
// lexicographically without using
// library functions
int stringCompare(const string& str1, const string& str2)
{
    int l1 = str1.length();
    int l2 = str2.length();
    int lmin = min(l1, l2);
 
    for (int i = 0; i < lmin; i++) {
        int str1_ch = (int)(str1[i]);
        int str2_ch = (int)(str2[i]);
 
        if (str1_ch != str2_ch) {
            return str1_ch - str2_ch;
        }
    }
 
    // Edge case for strings like
    // String1 = "Geeks" and
    // String2 = "Geeksforgeeks"
    if (l1 != l2) {
        return l1 - l2;
    }
 
    // If none of the above conditions
    // is true, it implies both the
    // strings are equal
    else {
        return 0;
    }
}
 
void isEqual(const string& str1, const string& str2)
{
    int compareStrings = stringCompare(str1, str2);
    bool result = (compareStrings == 0) ? true : false;
 
    // Comparing for String 1 < String 2
    cout << "Comparing " << str1 << " and " << str2 << ": ";
    if (result)
        cout << "Equal" << endl;
    else
        cout << "Not equal" << endl;
}
 
int main()
{
    string string1 = "Geeksforgeeks";
    string string2 = "Practice";
    string string3 = "Geeks";
    string string4 = "Geeks";
 
    // Comparing for String 1 < String 2
    isEqual(string1, string2);
 
    // Comparing for String 3 = String 4
    isEqual(string3, string4);
 
    // Comparing for String 1 > String 4
    isEqual(string1, string4);
 
    return 0;
}
// This code is contributed by Prasad264

Java

// Java program to Compare two strings
// lexicographically
public class GFG {
 
    // This method compares two strings
    // lexicographically without using
    // library functions
    public static int stringCompare(String str1,
                                    String str2)
    {
 
        int l1 = str1.length();
        int l2 = str2.length();
        int lmin = Math.min(l1, l2);
 
        for (int i = 0; i < lmin; i++) {
            int str1_ch = (int)str1.charAt(i);
            int str2_ch = (int)str2.charAt(i);
 
            if (str1_ch != str2_ch) {
                return str1_ch - str2_ch;
            }
        }
 
        // Edge case for strings like
        // String1 = "Geeks" and
        // String2 = "Geeksforgeeks"
        if (l1 != l2) {
            return l1 - l2;
        }
 
        // If none of the above conditions
        // is true, it implies both the
        // strings are equal
        else {
            return 0;
        }
    }
 
    public static void isEqual(String str1, String str2)
    {
        int compareStrings = stringCompare(str1, str2);
        Boolean result
            = (compareStrings == 0) ? true : false;
 
        // Comparing for String 1 < String 2
        System.out.print("Comparing " + str1 + " and "
                         + str2 + ": ");
        if (result)
            System.out.println("Equal");
        else
            System.out.println("Not equal");
    }
 
    // Driver function to test the above program
    public static void main(String args[])
    {
        String string1 = new String("Geeksforgeeks");
        String string2 = new String("Practice");
        String string3 = new String("Geeks");
        String string4 = new String("Geeks");
 
        // Comparing for String 1 < String 2
        isEqual(string1, string2);
 
        // Comparing for String 3 = String 4
        isEqual(string3, string4);
 
        // Comparing for String 1 > String 4
        isEqual(string1, string4);
    }
}

Python3

# This function compares two strings
# lexicographically without using
# library functions
def stringCompare(str1, str2):
    l1 = len(str1)
    l2 = len(str2)
    lmin = min(l1, l2)
 
    for i in range(lmin):
        str1_ch = ord(str1[i])
        str2_ch = ord(str2[i])
 
        if str1_ch != str2_ch:
            return str1_ch - str2_ch
 
    # Edge case for strings like
    # String1 = "Geeks" and
    # String2 = "Geeksforgeeks"
    if l1 != l2:
        return l1 - l2
 
    # If none of the above conditions
    # is true, it implies both the
    # strings are equal
    else:
        return 0
 
def is_equal(str1, str2):
    compare_strings = stringCompare(str1, str2)
    result = compare_strings == 0
 
    # Comparing for String 1 < String 2
    print(f"Comparing {str1} and {str2}: ", end="")
    if result:
        print("Equal")
    else:
        print("Not equal")
 
string1 = "Geeksforgeeks"
string2 = "Practice"
string3 = "Geeks"
string4 = "Geeks"
 
# Comparing for String 1 < String 2
is_equal(string1, string2)
 
# Comparing for String 3 = String 4
is_equal(string3, string4)
 
# Comparing for String 1 > String 4
is_equal(string1, string4)

C#

// C# Code
 
using System;
 
public class GFG
{
    // This method compares two strings
    // lexicographically without using
    // library functions
    public static int StringCompare(string str1, string str2)
    {
        int l1 = str1.Length;
        int l2 = str2.Length;
        int lmin = Math.Min(l1, l2);
 
        for (int i = 0; i < lmin; i++)
        {
            int str1_ch = (int)str1[i];
            int str2_ch = (int)str2[i];
 
            if (str1_ch != str2_ch)
            {
                return str1_ch - str2_ch;
            }
        }
 
        // Edge case for strings like
        // String1 = "Geeks" and
        // String2 = "Geeksforgeeks"
        if (l1 != l2)
        {
            return l1 - l2;
        }
 
        // If none of the above conditions
        // is true, it implies both the
        // strings are equal
        else
        {
            return 0;
        }
    }
 
    public static void IsEqual(string str1, string str2)
    {
        int compareStrings = StringCompare(str1, str2);
        bool result = (compareStrings == 0) ? true : false;
 
        // Comparing for String 1 < String 2
        Console.Write("Comparing " + str1 + " and " + str2 + ": ");
        if (result)
            Console.WriteLine("Equal");
        else
            Console.WriteLine("Not equal");
    }
 
    // Driver function to test the above program
    public static void Main(string[] args)
    {
        string string1 = "Geeksforgeeks";
        string string2 = "Practice";
        string string3 = "Geeks";
        string string4 = "Geeks";
 
        // Comparing for String 1 < String 2
        IsEqual(string1, string2);
 
        // Comparing for String 3 = String 4
        IsEqual(string3, string4);
 
        // Comparing for String 1 > String 4
        IsEqual(string1, string4);
    }
}

Javascript

function stringCompare(str1, str2) {
    const l1 = str1.length;
    const l2 = str2.length;
    const lmin = Math.min(l1, l2);
 
    for (let i = 0; i < lmin; i++) {
        const str1_ch = str1.charCodeAt(i);
        const str2_ch = str2.charCodeAt(i);
 
        if (str1_ch !== str2_ch) {
            return str1_ch - str2_ch;
        }
    }
 
    // Edge case for strings with different lengths
    if (l1 !== l2) {
        return l1 - l2;
    }
 
    // If none of the above conditions is true, it implies both strings are equal
    return 0;
}
 
function isEqual(str1, str2) {
    const compareStrings = stringCompare(str1, str2);
    const result = compareStrings === 0;
 
    console.log(`Comparing ${str1} and ${str2}: ${result ? 'Equal' : 'Not equal'}`);
}
 
const string1 = "Geeksforgeeks";
const string2 = "Practice";
const string3 = "Geeks";
const string4 = "Geeks";
 
// Comparing for String 1 < String 2
isEqual(string1, string2);
 
// Comparing for String 3 = String 4
isEqual(string3, string4);
 
// Comparing for String 1 > String 4
isEqual(string1, string4);

Output

Comparing Geeksforgeeks and Practice: Not equal
Comparing Geeks and Geeks: Equal
Comparing Geeksforgeeks and Geeks: Not equal













Reffered: https://www.geeksforgeeks.org


DSA

Related
Maximum number of soldier in a team Maximum number of soldier in a team
Time Crunch Challenge Time Crunch Challenge
Evaluate the Fractions Evaluate the Fractions
Minimum difference between components of the Graph after each query Minimum difference between components of the Graph after each query
Shortest path in grid with obstacles Shortest path in grid with obstacles

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14