Program to check if two strings are same or not
Last Updated :
14 Sep, 2023
Given two strings, the task is to check if these two strings are identical(same) 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
Brute Force Way:
The Approach:
Using (==) Operator in C++/Java and using (is) operator in Python.
C++
#include <iostream>
#include<string>
#include<bits/stdc++.h>
using namespace std;
int main() {
string s1 = "GeeksforGeeks" ;
string s2 = "Geeks for geeks" ;
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) {
String s1 = "GeeksforGeeks" ;
String s2 = "Geeks for geeks" ;
if (s1.equals(s2)) {
System.out.println( "Strings Are Equal" );
} else {
System.out.println( "Strings Are Not Equal" );
}
}
}
|
Python3
if __name__ = = "__main__" :
string1 = "GeeksforGeeks"
string2 = "Geeks for geeks"
if (string1 is string2):
print ( "Strings Are Equal" )
else :
print ( "Strings Are Not Equal" )
|
C#
using System;
class GFG
{
static void Main( string [] args)
{
string s1 = "GeeksforGeeks" ;
string s2 = "Geeks for geeks" ;
if (s1 == s2)
{
Console.WriteLine( "Strings Are Equal" );
}
else
{
Console.WriteLine( "Strings Are Not Equal" );
}
}
}
|
Javascript
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
Complexity Analysis:
Time Complexity: O(1).
Auxiliary Space: O(1).
Approach 1:
This can be done with the help of strcmp() method in C. Please note, this method is case-sensitive.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
char string1[100], string2[100];
cin >> string1;
cout << "Enter the first string: " << string1;
cin >> string2;
cout << "\nEnter the second string: " << string2;
cout << "\nAre both strings same: " ;
if ( strcmp (string1, string2) == 0) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
C
#include <stdio.h>
#include <string.h>
int main()
{
char string1[100], string2[100];
scanf ( "%[^\n]\ns" , string1);
printf ( "Enter the first string: %s" , string1);
scanf ( "%[^\n]\ns" , string2);
printf ( "\nEnter the second string: %s" , string2);
printf ( "\nAre both strings same: " );
if ( strcmp (string1, string2) == 0) {
printf ( "Yes" );
}
else {
printf ( "No" );
}
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String string1 = in.nextLine();
System.out.println( "Enter the first string: "
+ string1);
String string2 = in.nextLine();
System.out.println( "Enter the second string :"
+ string2);
System.out.println( "\nAre both strings same: " );
if (string1.equals(string2) == true ) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
|
Python 3
if __name__ = = "__main__" :
string1 = input ( "Enter the first string: " )
print (string1, end = "\n" )
string2 = input ( "Enter the second string: " )
print (string2, end = "\n" )
print ( "Are both strings same: " , end = " " )
if (string1 = = string2):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
public static void Main()
{
String string1 = Console.ReadLine();
Console.WriteLine( "Enter the first string: "
+ string1);
String string2 = Console.ReadLine();
Console.WriteLine( "Enter the second string :"
+ string2);
Console.WriteLine( "\nAre both strings same: " );
if (string1.Equals(string2) == true ) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
}
}
|
Javascript
const readline = require( 'readline' );
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let string1, string2;
rl.question( 'Enter the first string: ' , (input) => {
string1 = input;
rl.question( 'Enter the second string: ' , (input) => {
string2 = input;
console.log( 'Are both strings same: ' + (string1 === string2 ? 'Yes' : 'No' ));
rl.close();
});
});
|
Output
Enter the first string: ��}��
Enter the second string: �`
Are both strings same: No
Time Complexity: O(min(a,b)) // a is the length of the first string and b is the length of the second string.
Auxiliary Space: O(100)
Approach 2 : (Using two pointer approach)
The problem can be easily solved using two pointer approach. But before using two pointer one basic check that can be performed is the length. As it is very obvious to be same they will contain same length. If both of their length is same then we can perform the 2 pointer technique.
Output
Enter the first string:
Enter the second string:
Are both strings same: Yes
Time Complexity: O(N), for traversing using two pointers over the string in case their size is equal
Auxiliary Space: O(1), no extra space is used
Approach-3 : Using not equal to(!=) operator
Using the not equal to operator we can check whether both of the strings are equal or not.
C++
#include <iostream>
#include<string>
using namespace std;
int main() {
string s1 = "GeeksforGeeks" ;
string s2 = "Geeks for geeks" ;
if (s1!=s2){
cout<< "Strings Are Not Equal" <<endl;
} else {
cout<< "Strings Are Equal" <<endl;
}
return 0;
}
|
Java
import java.io.*;
public class Main {
public static void main(String[] args)
{
String s1 = "GeeksforGeeks" ;
String s2 = "Geeks for geeks" ;
if (!s1.equals(s2)) {
System.out.println( "Strings Are Not Equal" );
}
else {
System.out.println( "Strings Are Equal" );
}
}
}
|
Python3
def is_string_same(str1, str2):
if str1 ! = str2:
return 0
else :
return 1
s1 = "GeeksforGeeks"
s2 = "Geeks for geeks"
result = is_string_same(s1, s2)
if result = = 0 :
print ( "Strings are Not Equal" )
else :
print ( "Strings are Equal" )
|
C#
using System;
class Gfg {
static void Main( string [] args) {
string s1 = "GeeksforGeeks" ;
string s2 = "Geeks for geeks" ;
if (s1 != s2) {
Console.WriteLine( "Strings Are Not Equal" );
} else {
Console.WriteLine( "Strings Are Equal" );
}
}
}
|
Javascript
let s1 = "GeeksforGeeks" ;
let s2 = "Geeks for geeks" ;
if (s1 !== s2) {
console.log( "Strings Are Not Equal" );
} else {
console.log( "Strings Are Equal" );
}
|
Output
Strings Are Not Equal
Complexity Analysis:
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...