Pergunta de entrevista da empresa Google

Implement int strncmp(const char* s1, const char* s2, int n)

Respostas da entrevista

Sigiloso

10 de abr. de 2015

public class Strncmp { public static int strncmp(String s1,String s2,int n) { if(s1.length()s2.length()?s1.length():s2.length(); for(int i=0;is2.charAt(i)?1:-1; } else continue; } return 0; } public static void main(String[] args) { System.out.println(strncmp("swaraj","zaxzs",7)); } }

1

Sigiloso

3 de jun. de 2015

#include "stdafx.h" #include using namespace std; int mystrncmp(const char* s1, const char* s2, int n) { if (s1 == NULL || s2 == NULL) throw "Null argument(s)"; while (n > 0) { if (*s1 == 0) { if (*s2 == 0) return 0; //equal else return -1; //s2 is greater } else if (*s2 == 0) return 1; //s1 is greater else if (*s1 > *s2) return 1; else if (*s1 < *s2) return -1; s1++; s2++; n--; } return 0; } int _tmain(int argc, _TCHAR* argv[]) { cout << mystrncmp("sdf0-345", "dsf9074", 5) << endl; }