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 “w3wiki” and “w3wiki” 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; /* implementation of strcmp that ignores cases */ int ic_strcmp(string s1, string s2) { int i; for (i = 0; s1[i] && s2[i]; ++i) { /* If characters are same or inverting the 6th bit makes them same */ if (s1[i] == s2[i] || (s1[i] ^ 32) == s2[i]) continue ; else break ; } /* Compare the last (or first mismatching in case of not same) characters */ if (s1[i] == s2[i]) return 0; // Set the 6th bit in both, then compare if ((s1[i] | 32) < (s2[i] | 32)) return -1; return 1; } // Driver program to test above function int main() { cout<< "ret: " <<ic_strcmp( "Beginner" , "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( "w3wiki" , "w3wiki" )<<endl; cout<< "ret: " <<ic_strcmp( "w3wiki" , "w3wiki" )<<endl; return 0; } //This code is contributed by rathbhupendra |
C
#include <stdio.h> /* implementation of strcmp that ignores cases */ int ic_strcmp( char *s1, char *s2) { int i; for (i = 0; s1[i] && s2[i]; ++i) { /* If characters are same or inverting the 6th bit makes them same */ if (s1[i] == s2[i] || (s1[i] ^ 32) == s2[i]) continue ; else break ; } /* Compare the last (or first mismatching in case of not same) characters */ if (s1[i] == s2[i]) return 0; // Set the 6th bit in both, then compare if ((s1[i] | 32) < (s2[i] | 32)) return -1; return 1; } // Driver program to test above function int main( void ) { printf ( "ret: %d\n" , ic_strcmp( "Beginner" , "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( "w3wiki" , "w3wiki" )); printf ( "ret: %d\n" , ic_strcmp( "w3wiki" , "w3wiki" )); return 0; } |
Java
// Java program for the above approach import java.util.*; public class Main { /* implementation of strcmp that ignores cases */ public static int ic_strcmp(String s1, String s2) { int i; for (i = 0 ; i < s1.length() && i < s2.length(); ++i) { /* If characters are same or inverting the 6th bit makes them same */ if (s1.charAt(i) == s2.charAt(i) || (s1.charAt(i) ^ 32 ) == s2.charAt(i)) continue ; else break ; } /* Compare the last (or first mismatching in case of not same) characters */ if (i == s1.length() && i == s2.length()) return 0 ; // Set the 6th bit in both, then compare 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 ; } // Driver program to test above function public static void main(String[] args) { System.out.println( "ret: " + ic_strcmp( "Beginner" , "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( "w3wiki" , "w3wiki" )); System.out.println( "ret: " + ic_strcmp( "w3wiki" , "w3wiki" )); } } // This code is contributed by codebraxnzt |
Python3
# Python implementation of above approach # implementation of strcmp that ignores cases def ic_strcmp(s1, s2): i = 0 while i < len (s1) and i < len (s2): # If characters are same or inverting the 6th bit makes them same if s1[i] = = s2[i] or ord (s1[i]) ^ 32 = = ord (s2[i]): i + = 1 else : break # Compare the last (or first mismatching in case of not same) characters if i = = len (s1) and i = = len (s2): return 0 elif i = = len (s1): return - 1 elif i = = len (s2): return 1 # Set the 6th bit in both, then compare if ord (s1[i]) | 32 < ord (s2[i]) | 32 : return - 1 return 1 # Driver program to test above function if __name__ = = '__main__' : print ( "ret: " , ic_strcmp( "Beginner" , "apple" )) print ( "ret: " , ic_strcmp(" ", " ABCD")) print ( "ret: " , ic_strcmp( "ABCD" , "z" )) print ( "ret: " , ic_strcmp( "ABCD" , "abcdEghe" )) print ( "ret: " , ic_strcmp( "w3wiki" , "w3wiki" )) print ( "ret: " , ic_strcmp( "w3wiki" , "w3wiki" )) # This code is contributed by codebraxnzt |
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( "Beginner" , "apple" )); Console.WriteLine( "ret: " + IcStrCmp( "" , "ABCD" )); Console.WriteLine( "ret: " + IcStrCmp( "ABCD" , "z" )); Console.WriteLine( "ret: " + IcStrCmp( "ABCD" , "abcdEghe" )); Console.WriteLine( "ret: " + IcStrCmp( "w3wiki" , "w3wiki" )); Console.WriteLine( "ret: " + IcStrCmp( "w3wiki" , "w3wiki" )); } } |
Javascript
// Javascript program for the above approach // implementation of strcmp that ignores cases function ic_strcmp(s1, s2) { let i = 0; while (i < s1.length && i < s2.length) { // If characters are same or inverting the 6th bit makes them same if (s1[i] === s2[i] || (s1.charCodeAt(i) ^ 32) === s2.charCodeAt(i)) { i += 1; } else { break ; } } // Compare the last (or first mismatching in case of not same) characters if (i === s1.length && i === s2.length) { return 0; } else if (i === s1.length) { return -1; } else if (i === s2.length) { return 1; } // Set the 6th bit in both, then compare if ((s1.charCodeAt(i) | 32) < (s2.charCodeAt(i) | 32)) { return -1; } return 1; } // Driver program to test above function console.log( "ret: " , ic_strcmp( "Beginner" , "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( "w3wiki" , "w3wiki" )); console.log( "ret: " , ic_strcmp( "w3wiki" , "w3wiki" )); // This code is contributed by adityashatmfh |
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.