Write your own strcmp that ignores cases
Write a modified strcmp function which ignores cases and returns -1 if s1 < s2, 0 if s1 = s2, else returns 1. For example, your strcmp should consider “GeeksforGeeks” and “geeksforgeeks” as same string.
Source: Microsoft Interview Set 5
Following solution assumes that characters are represented using ASCII representation, i.e., codes for ‘a’, ‘b’, ‘c’, … ‘z’ are 97, 98, 99, … 122 respectively. And codes for ‘A’, “B”, ‘C’, … ‘Z’ are 65, 66, … 90 respectively.
Following are the detailed steps.
1) Iterate through every character of both strings and do following for each character.
…a) If str1[i] is same as str2[i], then continue.
…b) If inverting the 6th least significant bit of str1[i] makes it same as str2[i], then continue. For example, if str1[i] is 65, then inverting the 6th bit will make it 97. And if str1[i] is 97, then inverting the 6th bit will make it 65.
…c) If any of the above two conditions is not true, then break.
2) Compare the last (or first mismatching in case of not same) characters.
C++
#include <bits/stdc++.h>
using namespace std;
int ic_strcmp(string s1, string s2)
{
int i;
for (i = 0; s1[i] && s2[i]; ++i)
{
if (s1[i] == s2[i] || (s1[i] ^ 32) == s2[i])
continue ;
else
break ;
}
if (s1[i] == s2[i])
return 0;
if ((s1[i] | 32) < (s2[i] | 32))
return -1;
return 1;
}
int main()
{
cout<< "ret: " <<ic_strcmp( "Geeks" , "apple" ) <<endl;
cout<< "ret: " <<ic_strcmp( "" , "ABCD" )<<endl;
cout<< "ret: " <<ic_strcmp( "ABCD" , "z" )<<endl;
cout<< "ret: " <<ic_strcmp( "ABCD" , "abcdEghe" )<<endl;
cout<< "ret: " <<ic_strcmp( "GeeksForGeeks" , "gEEksFORGeEKs" )<<endl;
cout<< "ret: " <<ic_strcmp( "GeeksForGeeks" , "geeksForGeeks" )<<endl;
return 0;
}
|
C
#include <stdio.h>
int ic_strcmp( char *s1, char *s2)
{
int i;
for (i = 0; s1[i] && s2[i]; ++i)
{
if (s1[i] == s2[i] || (s1[i] ^ 32) == s2[i])
continue ;
else
break ;
}
if (s1[i] == s2[i])
return 0;
if ((s1[i] | 32) < (s2[i] | 32))
return -1;
return 1;
}
int main( void )
{
printf ( "ret: %d\n" , ic_strcmp( "Geeks" , "apple" ));
printf ( "ret: %d\n" , ic_strcmp( "" , "ABCD" ));
printf ( "ret: %d\n" , ic_strcmp( "ABCD" , "z" ));
printf ( "ret: %d\n" , ic_strcmp( "ABCD" , "abcdEghe" ));
printf ( "ret: %d\n" , ic_strcmp( "GeeksForGeeks" , "gEEksFORGeEKs" ));
printf ( "ret: %d\n" , ic_strcmp( "GeeksForGeeks" , "geeksForGeeks" ));
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int ic_strcmp(String s1, String s2) {
int i;
for (i = 0 ; i < s1.length() && i < s2.length(); ++i) {
if (s1.charAt(i) == s2.charAt(i) || (s1.charAt(i) ^ 32 ) == s2.charAt(i))
continue ;
else
break ;
}
if (i == s1.length() && i == s2.length())
return 0 ;
if (i == s1.length())
return - 1 ;
else if (i == s2.length())
return 1 ;
else if ((s1.charAt(i) | 32 ) < (s2.charAt(i) | 32 ))
return - 1 ;
return 1 ;
}
public static void main(String[] args) {
System.out.println( "ret: " + ic_strcmp( "Geeks" , "apple" ));
System.out.println( "ret: " + ic_strcmp( "" , "ABCD" ));
System.out.println( "ret: " + ic_strcmp( "ABCD" , "z" ));
System.out.println( "ret: " + ic_strcmp( "ABCD" , "abcdEghe" ));
System.out.println( "ret: " + ic_strcmp( "GeeksForGeeks" , "gEEksFORGeEKs" ));
System.out.println( "ret: " + ic_strcmp( "GeeksForGeeks" , "geeksForGeeks" ));
}
}
|
Python3
def ic_strcmp(s1, s2):
i = 0
while i < len (s1) and i < len (s2):
if s1[i] = = s2[i] or ord (s1[i]) ^ 32 = = ord (s2[i]):
i + = 1
else :
break
if i = = len (s1) and i = = len (s2):
return 0
elif i = = len (s1):
return - 1
elif i = = len (s2):
return 1
if ord (s1[i]) | 32 < ord (s2[i]) | 32 :
return - 1
return 1
if __name__ = = '__main__' :
print ( "ret: " , ic_strcmp( "Geeks" , "apple" ))
print ( "ret: " , ic_strcmp(" ", " ABCD"))
print ( "ret: " , ic_strcmp( "ABCD" , "z" ))
print ( "ret: " , ic_strcmp( "ABCD" , "abcdEghe" ))
print ( "ret: " , ic_strcmp( "GeeksForGeeks" , "gEEksFORGeEKs" ))
print ( "ret: " , ic_strcmp( "GeeksForGeeks" , "geeksForGeeks" ))
|
C#
using System;
public class GFG{
static int IcStrCmp( string s1, string s2)
{
int i;
for (i = 0; i < s1.Length && i < s2.Length; ++i)
{
if ( char .ToLower(s1[i]) == char .ToLower(s2[i]))
{
continue ;
}
else
{
break ;
}
}
if (i == s1.Length && i == s2.Length)
{
return 0;
}
if (i == s1.Length)
{
return -1;
}
else if (i == s2.Length)
{
return 1;
}
else if ( char .ToLower(s1[i]) < char .ToLower(s2[i]))
{
return -1;
}
return 1;
}
static void Main( string [] args)
{
Console.WriteLine( "ret: " + IcStrCmp( "Geeks" , "apple" ));
Console.WriteLine( "ret: " + IcStrCmp( "" , "ABCD" ));
Console.WriteLine( "ret: " + IcStrCmp( "ABCD" , "z" ));
Console.WriteLine( "ret: " + IcStrCmp( "ABCD" , "abcdEghe" ));
Console.WriteLine( "ret: " + IcStrCmp( "GeeksForGeeks" , "gEEksFORGeEKs" ));
Console.WriteLine( "ret: " + IcStrCmp( "GeeksForGeeks" , "geeksForGeeks" ));
}
}
|
Javascript
function ic_strcmp(s1, s2) {
let i = 0;
while (i < s1.length && i < s2.length) {
if (s1[i] === s2[i] || (s1.charCodeAt(i) ^ 32) === s2.charCodeAt(i)) {
i += 1;
} else {
break ;
}
}
if (i === s1.length && i === s2.length) {
return 0;
} else if (i === s1.length) {
return -1;
} else if (i === s2.length) {
return 1;
}
if ((s1.charCodeAt(i) | 32) < (s2.charCodeAt(i) | 32)) {
return -1;
}
return 1;
}
console.log( "ret: " , ic_strcmp( "Geeks" , "apple" ));
console.log( "ret: " , ic_strcmp( "" , "ABCD" ));
console.log( "ret: " , ic_strcmp( "ABCD" , "z" ));
console.log( "ret: " , ic_strcmp( "ABCD" , "abcdEghe" ));
console.log( "ret: " , ic_strcmp( "GeeksForGeeks" , "gEEksFORGeEKs" ));
console.log( "ret: " , ic_strcmp( "GeeksForGeeks" , "geeksForGeeks" ));
|
Output
ret: 1
ret: -1
ret: -1
ret: -1
ret: 0
ret: 0
Time Complexity: O(min(|s1|, |s2|))
Auxiliary Space: O(1)
This article is compiled by Narendra Kangralkar.
Last Updated :
02 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...