Open In App

Check if two strings are same or not

Last Updated : 03 Mar, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given two strings, the task is to check if these two strings are identical(same) or not. Consider case sensitivity.

Examples:

Input: s1 = “abc”, s2 = “abc” 
Output: Yes 

Input: s1 = “”, s2 = “” 
Output: Yes 

Input: s1 = “GeeksforGeeks”, s2 = “Geeks” 
Output: No 

Approach – By Using (==) in C++/Python/C#, equals in Java and === in JavaScript

This approach compares two strings to check if they are the same. It works by using the equality operator (==) to compare both strings character by character. If the strings are identical, it prints "Yes", otherwise, it prints "No". The comparison stops as soon as a mismatch is found, or if one string ends before the other. The program performs this comparison in a straightforward way without any extra steps, simply checking the content of the strings.

C++
#include <bits/stdc++.h>
using namespace std;

// Function to compare both strings directly
bool areStringsSame(string s1, string s2) {
    return s1 == s2;
}

int main()
{
    string s1 = "abc";
    string s2 = "abcd";
  
    // Call the areStringsSame function to compare strings
    if (areStringsSame(s1, s2)) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }

    return 0;
}
C Java Python C# JavaScript

Output
No

Time Complexity: O(n)
Auxiliary Space: O(1)

Approach – By Using String Comparison Functions

In this approach, the program compares two strings to check if they are identical. It uses the strcmp function, which compares the strings character by character. If the strings are exactly the same, strcmp returns 0, indicating no difference between them. If the strings are different, strcmp returns a non-zero value. Based on this result, the program prints "Yes" if the strings match and "No" if they don’t. The comparison stops as soon as a mismatch is found or the strings end.

C++
#include <bits/stdc++.h>

using namespace std;

// Function to compare two strings using strcmp
bool areStringsSame(char s1[], char s2[]) {
    return strcmp(s1, s2) == 0;
}

int main() {
    char s1[] = "hello";
    char s2[] = "hello";

    // Call the areStringsSame function to compare strings
    if (areStringsSame(s1, s2)) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;  
    }

    return 0;
}
C Java Python C# JavaScript

Output
Yes

Time Complexity: O(n)
Auxiliary Space: O(1)

Approach – By Writing your Own Method

In this approach, the program compares two strings by first checking if their lengths are the same. If the lengths differ, the strings cannot be identical, so it returns false. If the lengths are the same, it then compares each character of the two strings one by one. If any mismatch is found, it returns false, indicating the strings are not the same. If no differences are found throughout the comparison, it returns true, meaning the strings are identical.

C++
#include <bits/stdc++.h>

using namespace std;

bool areStringsEqual(string &s1, string &s2) {
  
    // Compare lengths first
    if (s1.length() != s2.length()) {
        return false;
    }

    // Compare character by character
    for (size_t i = 0; i < s1.length(); ++i) {
        if (s1[i] != s2[i]) {
            return false; 
        }
    }

    return true; 
}

int main() {
    string s1 = "hello";
    string s2 = "hello";

    if (areStringsEqual(s1, s2)) {
        cout << "Yes" << endl; 
    } else {
        cout << "No" << endl;  
    }

    return 0;
}
C Java Python JavaScript

Output
Yes

Time Complexity: O(n)
Auxiliary Space: O(1)




Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg