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, … 95 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.
ret: 1 ret: -1 ret: -1 ret: -1 ret: 0 ret: 0
This article is compiled by Narendra Kangralkar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Check if two strings are same ignoring their cases
- Write your own atoi()
- Write a program to print all permutations of a given string
- Count of lines required to write the given String
- Write a program to reverse an array or string
- Write an Efficient C Program to Reverse Bits of a Number
- Write an Efficient Method to Check if a Number is Multiple of 3
- Write a function that returns 2 for input 1 and returns 1 for 2
- Find the last remaining Character in the Binary String according to the given conditions
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Reverse the substrings of the given String according to the given Array of indices
- Check if the Depth of Parentheses is correct in the given String
- Python | Replacing Nth occurrence of multiple characters in a String with the given character
- Longest palindromic String formed using concatenation of given strings in any order
Improved By : rathbhupendra