3. Look up the man pages for the following string functions from the C standard library, and write implementations of each:
Please explain your answer, thank you
Solution
#include <stdio.h>
int strcmp(const char *S1, const
char *S2)
{
while (*S1)
{
if (*S1!= *S2)
break;
S1++;
S2++;
}
return *(const unsigned char*)S1-
*(const unsigned char*)S2;
}
int main()
{
char *A = "Student";
char *B = "Stand";
int cmp = strcmp(A, B);
if (cmp > 0)
printf("%s is greater than %s. Character in %s is lexicographically
after the character of %s",A,B,A,B);
else if (cmp < 0)
printf("%s is less than %s. Character in %s is lexicographically
before the character of %s",A,B,A,B);
else
printf("%s is equal to %s",A,B);
return 0;
}
Code Screenshot
Output
Get Answers For Free
Most questions answered within 1 hours.