Implement strcmp

Answers were Sorted based on User's Feedback



Implement strcmp..

Answer / lylez00

#include <string.h>
/* strcmp */
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);
return ((uc1 < uc2) ? -1 : (uc1 > uc2));
}

Is This Answer Correct ?    7 Yes 3 No

Implement strcmp..

Answer / shanmugavalli

int strcmp(const char *src, const char *dest)
{

while (*src && *dest && (*src==*dest))
{
src++;
dest++;
}
return (*src-*dest);

}

Is This Answer Correct ?    1 Yes 1 No

Implement strcmp..

Answer / sanjith

#inclde<string.h>
int d;
class test
public: void read()
void cmb()
};
void test::read()
{ cout<<"Enter the first string:";
cin>>s1;
cout<<"Enter the second string";
cin>>s2;
}
void test::cmb()
{
d=strcmp(s1,s2);
}
main()
{
test t1,t2;
t1.read();
t2.cmb();
t2.print();
}

Is This Answer Correct ?    1 Yes 4 No

Post New Answer

More C++ General Interview Questions

What is srand c++?

0 Answers  


What is lazy initialization in c++?

0 Answers  


Find out the bug in this code,because of that this code will not compile....... #include <iostream> #include <new> #include <cstring> using namespace std; class balance { double cur_bal; char name[80]; public: balance(double n, char *s) { cur_bal = n; strcpy(name, s); } ~balance() { cout << "Destructing "; cout << name << "\n"; } void set(double n, char *s) { cur_bal = n; strcpy(name, s); } void get_bal(double &n, char *s) { n = cur_bal; strcpy(s, name); } }; int main() { balance *p; char s[80]; double n; int i; try { p = new balance [3]; // allocate entire array } catch (bad_alloc xa) { cout << "Allocation Failure\n"; return 1; }

2 Answers   Impetus,


What is "map" in STL?

2 Answers  


Give 2 examples of a code optimization?

4 Answers  






What is atoi?

0 Answers  


Difference between shift left and shift right?

1 Answers   Symphony,


Why is c++ considered difficult?

0 Answers  


Why do we use iterators?

0 Answers  


What are the three forms of cin.get() and what are their differences?

0 Answers  


Name the implicit member functions of a class.

0 Answers  


If you want to share several functions or variables in several files maitaining the consistency how would you share it?

0 Answers  


Categories