Microsoft Interview Question
Software Engineer in TestsCompare_Strings(char* str1, char* str2)
{
while( *str1 && *str2)
{
int diff=*str1-*str2;
if(*str1 >='A' && *str1 <='Z') diff+=32;
if(*str2 >='A' && *str2 <='Z') diff-=32;
if(diff >0) return 1;
else if(diff < 0) return -1;
str1++;
str2++;
}
return ((*str1 - *str2)>0?1:((*str1 - *str2)<0)?-1:0);
}
int (strcmp)(const char *s1, const char *s2)
{
unsigned char uc1, uc2;
/* Move s1 and s2 to the first differing characters
in each string, or the ends of the strings if they
are identical. */
while (*s1 != '\0' && *s1 == *s2) {
s1++;
s2++;
}
/* Compare the characters as unsigned char and
return the difference. */
uc1 = (*(unsigned char *) s1);
uc2 = (*(unsigned char *) s2);
if (uc1 != uc2)
return ((uc1 < uc2) ? -1 : (uc1 > uc2));
else
return 0;
}
char toLower(char c){
..if ((c>='A')&&(c<='Z'))
....return (c-'A'+'a');
}
int compare(char* a, char*b)
{
..if (a==b==NULL)
....return 0;
..if (a==NULL)
....return -1;
..if (b==NULL)
....return 1;
..while (*a&&*b){
....if (toLower(*a)>toLower(*b))
......return 1;
....else if (toLower(*a)<toLower(*b))
......return -1;
....else{
......a++;
......b++;
....}
..}
..if ((*a==NULL)&&(*b==NULL))
....return 0;
..else if (*a==NULL)
....return -1;
..else //(*b==NULL)
....return 1;
}
void main()
{
..cout<<compare("AB", "Ab");
}
//c# way. Here is the code with the explaination.
//First convert to char
char[] c1 = s1.ToCharArray();
char[] c2 = s2.ToCharArray();
//Check the length is same or not
if (c1.Length != c2.Length) throw new Exception("Length is not same. Lenght shoulb be same");
//Convert to lower case
if (c1[0] <= 'Z')
{
c1[0] = (char)((int)c1[0] + 32);
}
if (c2[0] <= 'Z')
{
c2[0] = (char)((int)c2[0] + 32);
}
//Now compare the first character
//One assumption is:
//1. The string is always sorted that means it is like abc, bcd, def, mno, etc...but not like CBG, GAB, etc..
if (c1[0] > c2[0]) return 1;
else if (c1[0] < c2[0]) return -1;
else return 0;
Any comment?
int strcmp11 (const char * src,const char * dst)
{
int ret = 0 ;
if(src == NULL || dst == NULL)
{
std::cout << "NULL Detected";
return -3;
}
while( ! (ret =(unsigned char) ::toupper(*src) - (unsigned char)::toupper(*dst)) && *dst)
++src, ++dst;
if ( ret < 0 )
ret = -1 ;
else if ( ret > 0 )
ret = 1 ;
return( ret );
}
Another solution..which to me seemed elegent. Its from a cookbook By Herb's
int stringcmp_ign_case(const char* str1,const char* str2)
{
while(*str1 && *str2){
if(tolower(*str1)!=tolower(*str2))
break;
*str1++;*str2++;
}
//this elegantly handles the three cases
//NULL-NULL=0
//Somevalue-NUll>1
//NULL-somevalue<1
return tolower(*str1)-tolower(*str2);
}
Pretty similar to Ankush's solution
For the above algorithm, one needs to check for all the characters present in both the strings if the ASCII value of any one of the characters in string 1 is less than we must return -1 if both the strings have equal length and both are equal then return 0 else return 1.
Implementation:
#include<bits/stdc++.h>
using namespace std;
int findstrings(string str1, string str2){
int count = 0;
transform(str1.begin(), str1.end(), str1.begin(), ::tolower);
transform(str2.begin(), str2.end(), str2.begin(), ::tolower);
int n = str1.length();
int m = str2.length();
if(n > m)
return -1;
for(int i = 0; i < n; i++){
if(str1[i] == str2[i])
count++;
else if((int)str1[i] > (int)str2[i])
return +1;
else
return -1;
}
if(count == n)
return 0;
}
int main(){
string str1 = "abc";
string str2 = "mno";
cout<<findstrings(str1, str2)<<endl;
return 0;
}
Some general testcases for this would be:
- acoader November 03, 2008Basic tests:
("abc","abc")
("abc","aBc")
("abc","ABC")
("abc","def")
("abc","aec")
Null tests:
(NULL,"abc")
("abc",NULL)
(NULL,NULL)
Boundary tests:
("abc",abd")
("dab","cab")
("Abc","abc")
("abc","abC")
("","abc")
("abc","")
("","")
Non alphabets:
("Aa-#Bb", "aa-#bb")
("*&^","!@$")
("*&^","*&^")
For each loop or condition, prepare a testcase that
meets one, two,.. all of the conditions
("$ABC",">abc") // If difference of 26 is used for equalizing upper & lowercases, then it should still see $,> as different
...
....