Check if two strings are same ignoring their cases
Given two strings str1 and str2. The task is to check if the two given strings are the same if a case-insensitive comparison is followed, i.e., the cases of the strings are ignored in Java.
Examples:
Input: str1 = "Geeks", str2 = "geeks"
Output: Same
Input: str1 = "Geek", str2 = "geeksforgeeks"
Output: Not Same
Method 1: Naive Approach
- Compare each character of the first string with the corresponding character of the second string.
- if it is matched, compare next character.
- If it does not match check if it is matched by ignoring their cases.
- If matched, compare next character.
- If all characters matched, return true
- If any character is not matched, return false.
The implementation is given below.
C++
#include <iostream>
using namespace std;
bool equalIgnoreCase(string str1, string str2)
{
int i = 0;
int len1 = str1.size();
int len2 = str2.size();
if (len1 != len2)
return false ;
while (i < len1) {
if (str1[i] == str2[i]) {
i++;
}
else if (!((str1[i] >= 'a' && str1[i] <= 'z' )
|| (str1[i] >= 'A' && str1[i] <= 'Z' ))) {
return false ;
}
else if (!((str2[i] >= 'a' && str2[i] <= 'z' )
|| (str2[i] >= 'A' && str2[i] <= 'Z' ))) {
return false ;
}
else {
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 ;
}
i++;
}
}
return true ;
}
void equalIgnoreCaseUtil(string str1, string str2)
{
bool res = equalIgnoreCase(str1, str2);
if (res == true )
cout << "Same" << endl;
else
cout << "Not Same" << endl;
}
int main()
{
string str1, str2;
str1 = "Geeks" ;
str2 = "geeks" ;
equalIgnoreCaseUtil(str1, str2);
str1 = "Geek" ;
str2 = "geeksforgeeks" ;
equalIgnoreCaseUtil(str1, str2);
return 0;
}
|
Java
class GFG
{
static boolean equalIgnoreCase(String str1, String str2)
{
int i = 0 ;
int len1 = str1.length();
int len2 = str2.length();
if (len1 != len2)
return false ;
while (i < len1)
{
if (str1.charAt(i) == str2.charAt(i))
{
i++;
}
else if (!((str1.charAt(i) >= 'a' && str1.charAt(i) <= 'z' )
|| (str1.charAt(i) >= 'A' && str1.charAt(i) <= 'Z' )))
{
return false ;
}
else if (!((str2.charAt(i) >= 'a' && str2.charAt(i) <= 'z' )
|| (str2.charAt(i) >= 'A' && str2.charAt(i) <= 'Z' )))
{
return false ;
}
else
{
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 ;
}
i++;
}
}
return true ;
}
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" );
}
public static void main(String args[])
{
String str1, str2;
str1 = "Geeks" ;
str2 = "geeks" ;
equalIgnoreCaseUtil(str1, str2);
str1 = "Geek" ;
str2 = "geeksforgeeks" ;
equalIgnoreCaseUtil(str1, str2);
}
}
|
Python3
def equalIgnoreCase(str1, str2):
i = 0
len1 = len (str1)
len2 = len (str2)
if (len1 ! = len2):
return False
while (i < len1):
if (str1[i] = = str2[i]):
i + = 1
elif (((str1[i] > = 'a' and str1[i] < = 'z' ) or
(str1[i] > = 'A' and str1[i] < = 'Z' )) = = False ):
return False
elif (((str2[i] > = 'a' and str2[i] < = 'z' ) or
(str2[i] > = 'A' and str2[i] < = 'Z' )) = = False ):
return False
else :
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
i + = 1
return True
def equalIgnoreCaseUtil(str1, str2):
res = equalIgnoreCase(str1, str2)
if (res = = True ):
print ( "Same" )
else :
print ( "Not Same" )
if __name__ = = '__main__' :
str1 = "Geeks"
str2 = "geeks"
equalIgnoreCaseUtil(str1, str2)
str1 = "Geek"
str2 = "geeksforgeeks"
equalIgnoreCaseUtil(str1, str2)
|
C#
using System;
class GFG
{
static bool equalIgnoreCase( string str1, string str2)
{
int i = 0;
int len1 = str1.Length;
int len2 = str2.Length;
if (len1 != len2)
return false ;
while (i < len1)
{
if (str1[i] == str2[i])
{
i++;
}
else if (!((str1[i] >= 'a' && str1[i] <= 'z' )
|| (str1[i] >= 'A' && str1[i] <= 'Z' )))
{
return false ;
}
else if (!((str2[i] >= 'a' && str2[i] <= 'z' )
|| (str2[i] >= 'A' && str2[i] <= 'Z' )))
{
return false ;
}
else
{
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 ;
}
i++;
}
}
return true ;
}
static void equalIgnoreCaseUtil( string str1, string str2)
{
bool res = equalIgnoreCase(str1, str2);
if (res == true )
Console.WriteLine( "Same" );
else
Console.WriteLine( "Not Same" );
}
public static void Main()
{
string str1, str2;
str1 = "Geeks" ;
str2 = "geeks" ;
equalIgnoreCaseUtil(str1, str2);
str1 = "Geek" ;
str2 = "geeksforgeeks" ;
equalIgnoreCaseUtil(str1, str2);
}
}
|
Javascript
<script>
function equalIgnoreCase(str1, str2)
{
let i = 0;
let len1 = str1.length;
let len2 = str2.length;
if (len1 != len2)
return false ;
while (i < len1)
{
if (str1[i] == str2[i])
{
i++;
}
else if (!((str1[i].charCodeAt() >= 'a' .charCodeAt() && str1[i].charCodeAt() <= 'z' .charCodeAt())
|| (str1[i].charCodeAt() >= 'A' .charCodeAt() && str1[i].charCodeAt() <= 'Z' .charCodeAt())))
{
return false ;
}
else if (!((str2[i].charCodeAt() >= 'a' .charCodeAt() && str2[i].charCodeAt() <= 'z' .charCodeAt())
|| (str2[i].charCodeAt() >= 'A' .charCodeAt() && str2[i].charCodeAt() <= 'Z' .charCodeAt())))
{
return false ;
}
else
{
if (str1[i].charCodeAt() >= 'a' .charCodeAt() && str1[i].charCodeAt() <= 'z' .charCodeAt())
{
if (str1[i].charCodeAt() - 32 != str2[i].charCodeAt())
return false ;
}
else if (str1[i].charCodeAt() >= 'A' .charCodeAt() && str1[i].charCodeAt() <= 'Z' .charCodeAt())
{
if (str1[i].charCodeAt() + 32 != str2[i].charCodeAt())
return false ;
}
i++;
}
}
return true ;
}
function equalIgnoreCaseUtil(str1, str2)
{
let res = equalIgnoreCase(str1, str2);
if (res == true )
document.write( "Same" + "</br>" );
else
document.write( "Not Same" + "</br>" );
}
let str1, str2;
str1 = "Geeks" ;
str2 = "geeks" ;
equalIgnoreCaseUtil(str1, str2);
str1 = "Geek" ;
str2 = "geeksforgeeks" ;
equalIgnoreCaseUtil(str1, str2);
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Method 2: Using toUpperCase() function
- Change all lowercase characters of both string to uppercase and compare them.
- If they are equal, print “same” otherwise print “Not Same”.
The implementation is given below.
C++
#include<bits/stdc++.h>
using namespace std;
bool equalIgnoreCase(string str1, string str2)
{
int i = 0;
transform(str1.begin(), str1.end(), str1.begin(), :: toupper );
transform(str2.begin(), str2.end(), str2.begin(), :: toupper );
int x = str1.compare(str2);
if (x != 0)
return false ;
else
return true ;
}
void equalIgnoreCaseUtil(string str1, string str2)
{
bool res = equalIgnoreCase(str1, str2);
if (res == true )
cout << "Same" << endl;
else
cout << "Not Same" << endl;
}
int main()
{
string str1, str2;
str1 = "Geeks" ;
str2 = "geeks" ;
equalIgnoreCaseUtil(str1, str2);
str1 = "Geek" ;
str2 = "geeksforgeeks" ;
equalIgnoreCaseUtil(str1, str2);
return 0;
}
|
Java
class GFG
{
static boolean equalIgnoreCase(String str1, String str2)
{
int i = 0 ;
str1 = str1.toUpperCase();
str2 = str2.toUpperCase();
int x = str1.compareTo(str2);
if (x != 0 )
{
return false ;
}
else
{
return true ;
}
}
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" );
}
}
public static void main(String[] args)
{
String str1, str2;
str1 = "Geeks" ;
str2 = "geeks" ;
equalIgnoreCaseUtil(str1, str2);
str1 = "Geek" ;
str2 = "geeksforgeeks" ;
equalIgnoreCaseUtil(str1, str2);
}
}
|
Python3
def equalIgnoreCase(str1, str2) :
str1 = str1.upper();
str2 = str2.upper();
x = str1 = = str2;
return x;
def equalIgnoreCaseUtil(str1, str2) :
res = equalIgnoreCase(str1, str2);
if (res = = True ) :
print ( "Same" );
else :
print ( "Not Same" );
if __name__ = = "__main__" :
str1 = "Geeks" ;
str2 = "geeks" ;
equalIgnoreCaseUtil(str1, str2);
str1 = "Geek" ;
str2 = "geeksforgeeks" ;
equalIgnoreCaseUtil(str1, str2);
|
C#
using System;
public class GFG
{
static bool equalIgnoreCase(String str1, String str2)
{
str1 = str1.ToUpper();
str2 = str2.ToUpper();
int x = str1.CompareTo(str2);
if (x != 0)
{
return false ;
}
else
{
return true ;
}
}
static void equalIgnoreCaseUtil(String str1, String str2)
{
bool res = equalIgnoreCase(str1, str2);
if (res == true )
{
Console.WriteLine( "Same" );
}
else
{
Console.WriteLine( "Not Same" );
}
}
public static void Main()
{
String str1, str2;
str1 = "Geeks" ;
str2 = "geeks" ;
equalIgnoreCaseUtil(str1, str2);
str1 = "Geek" ;
str2 = "geeksforgeeks" ;
equalIgnoreCaseUtil(str1, str2);
}
}
|
Javascript
<script>
function equalIgnoreCase(str1,str2)
{
let i = 0;
str1 = str1.toUpperCase();
str2 = str2.toUpperCase();
let x = str1 == (str2);
if (!x)
{
return false ;
}
else
{
return true ;
}
}
function equalIgnoreCaseUtil(str1,str2)
{
let res = equalIgnoreCase(str1, str2);
if (res == true )
{
document.write( "Same<br>" );
}
else
{
document.write( "Not Same<br>" );
}
}
let str1, str2;
str1 = "Geeks" ;
str2 = "geeks" ;
equalIgnoreCaseUtil(str1, str2);
str1 = "Geek" ;
str2 = "geeksforgeeks" ;
equalIgnoreCaseUtil(str1, str2);
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Method 3: Using toLowerCase() function
- Change all characters of both string to lowercase and compare them.
- If they are equal, print “same” otherwise print “Not Same”.
The implementation is given below.
C++
#include<bits/stdc++.h>
using namespace std;
bool equalIgnoreCase(string str1, string str2)
{
int i = 0;
transform(str1.begin(), str1.end(), str1.begin(), :: tolower );
transform(str2.begin(), str2.end(), str2.begin(), :: tolower );
int x = str1.compare(str2);
if (x != 0)
return false ;
else
return true ;
}
void equalIgnoreCaseUtil(string str1, string str2)
{
bool res = equalIgnoreCase(str1, str2);
if (res == true )
cout << "Same" << endl;
else
cout << "Not Same" << endl;
}
int main()
{
string str1, str2;
str1 = "Geeks" ;
str2 = "geeks" ;
equalIgnoreCaseUtil(str1, str2);
str1 = "Geek" ;
str2 = "geeksforgeeks" ;
equalIgnoreCaseUtil(str1, str2);
return 0;
}
|
Java
class GFG
{
static boolean equalIgnoreCase(String str1, String str2)
{
int i = 0 ;
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
int x = str1.compareTo(str2);
return x == 0 ;
}
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" );
}
public static void main(String[] args)
{
String str1, str2;
str1 = "Geeks" ;
str2 = "geeks" ;
equalIgnoreCaseUtil(str1, str2);
str1 = "Geek" ;
str2 = "geeksforgeeks" ;
equalIgnoreCaseUtil(str1, str2);
}
}
|
Python3
def equalIgnoreCase(str1, str2) :
str1 = str1.lower();
str2 = str2.lower();
x = str1 = = str2;
return x;
def equalIgnoreCaseUtil(str1, str2) :
res = equalIgnoreCase(str1, str2);
if (res = = True ) :
print ( "Same" );
else :
print ( "Not Same" );
if __name__ = = "__main__" :
str1 = "Geeks" ;
str2 = "geeks" ;
equalIgnoreCaseUtil(str1, str2);
str1 = "Geek" ;
str2 = "geeksforgeeks" ;
equalIgnoreCaseUtil(str1, str2);
|
C#
using System;
class GFG {
static bool equalIgnoreCase(String str1, String str2)
{
str1 = str1.ToLower();
str2 = str2.ToLower();
int x = str1.CompareTo(str2);
if (x != 0) {
return false ;
}
else {
return true ;
}
}
static void equalIgnoreCaseUtil(String str1,
String str2)
{
bool res = equalIgnoreCase(str1, str2);
if (res == true ) {
Console.WriteLine( "Same" );
}
else {
Console.WriteLine( "Not Same" );
}
}
public static void Main()
{
String str1, str2;
str1 = "Geeks" ;
str2 = "geeks" ;
equalIgnoreCaseUtil(str1, str2);
str1 = "Geek" ;
str2 = "geeksforgeeks" ;
equalIgnoreCaseUtil(str1, str2);
}
}
|
Javascript
<script>
function equalIgnoreCase(str1,str2)
{
let i = 0;
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
let x = (str1 == (str2));
return x == true ;
}
function equalIgnoreCaseUtil(str1,str2)
{
let res = equalIgnoreCase(str1, str2);
if (res == true )
document.write( "Same<br>" );
else
document.write( "Not Same<br>" );
}
let str1, str2;
str1 = "Geeks" ;
str2 = "geeks" ;
equalIgnoreCaseUtil(str1, str2);
str1 = "Geek" ;
str2 = "geeksforgeeks" ;
equalIgnoreCaseUtil(str1, str2);
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Last Updated :
29 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...