Bloomberg LP Interview Question
Software Engineer InternsCountry: United States
Interview Type: Phone Interview
int strcmp(char *a,char *b)
{
int cmp =-1;
While (*a == *b && *a!='\0' && *b!='\0')
{
a++;b++;
cmp=0;
}
if(*a=='\0' && *b=='\0' && cmp==0)
{
cmp= 0;
}
else if(*a<*b)
cmp= -1;
else
cmp= 1;
return cmp;
}
int mystrcmp(char *a, char *b)
{
int m=strlen(a), n=strlen(b);
if (m==0 && n==0)
return 0;
if (m==0)
return -1;
if (n==0)
return 1;
return mystrcmp(++a,++b);
}
#include<stdio.h>
int cmp(char *,char *);
int main()
{
char s[100],s1[100];
int d;
gets(s);
gets(s1);
d=cmp(s,s1);
if(d==1)
printf("equal");
else
printf("not equal");
}
int cmp(char *a,char *b)
{
int c;
while(*a!='\0'||*b!='\0')
{
if(*a==*b)
c=1;
else
{
c=0;
break;
}
*a++;
*b++;
}
return c;
}
package career.cup.bloomberg;
public class stringCmp {
public static void main(String args[]) {
System.out.println("String Compare "+strLocalCmp("Hello","Hello"));
}
private static boolean strLocalCmp(String string, String string2) {
// TODO Auto-generated method stub
if(string==null || string2 == null){
return false;
}
if(string.length() != string2.length()) {
return false;
}
for(int i=0;i<string.length();i++){
if(string.charAt(i) != string2.charAt(i)) {
return false;
}
}
return true;
}
}
Sorry. Please ignore my previous comment. I could not delete it.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int mystrcmp(char *a, char *b) {
if (a && b) {
while (*a == *b) {
if (*a == '\0' || *b == '\0') {
return 0;
}
a++;
b++;
}
return(*a - *b > 0 ? 1:-1);
}
else {
return 0;
}
}
void assertInt(int a, int b) {
if (a == b) {
printf("Passed\n");
}
else {
printf("Failed\n");
}
}
int main() {
char * a = "abc";
char * b = "def";
assertInt(strcmp(a, b), mystrcmp(a, b));
assertInt(strcmp("bcde", "bcde"), mystrcmp("bcde", "bcde"));
assertInt(strcmp("", "bcde"), mystrcmp("", "bcde"));
assertInt(strcmp("", ""), mystrcmp("", ""));
assertInt(strcmp("gde", ""), mystrcmp("gde", ""));
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
char *a = "this is good";
char *b = "this is good";
int cmp;
while(*a == *b && *a != '\0' && *b != '\0')
{
cmp = 0;
a++;
b++;
}
if(*a == '\0' && *b == '\0' && cmp ==0)
{
printf("equal\n");
}
else
{
printf("not equal\n");
}
}
- R@M3$H.N December 22, 2014