Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

int mystrncmp(const char *s11 const char *s2. int n The function compares up to

ID: 3887095 • Letter: I

Question

int mystrncmp(const char *s11 const char *s2. int n The function compares up to "n" characters of the string pointed to by "s" to the string pointed to by "s2 . The function returns an integer (1), (0), or (-1), accordingly as the string to by "s1" is greater than, equal to, or less than the string pointed to by "s2" in the irst "n" bytes. char *mystrncpy(char *dest.const char *src int n) The function copies no more than "n" characters from the null-terminated string pointed to by "src" to the memory pointed to by "dest". The destination shall be padded with null characters. The function returns the pointer "dest"; the function has no failure mode and no error return. int mystrrchr(const char s, int c) The function locates the last occurrence of "c" (converted to a char) in the string pointed to by "s". The terminating null character is considered to be part of the string. The function returns the position of "c" found, or (-1) if the character was not found.

Explanation / Answer

please refer below code

1)

#include<stdlib.h>
#include<stdio.h>

int mystrncmp(const char* s1,const char * s2,int n);
int main()
{
char source[20]="Amsterdam";
char target[20]="Mumbai";

int comp;
comp = mystrncmp(target,source,5);
if(comp == 1)
printf("s1 is greater than s2 ");

else if(comp == -1)
printf("s1 is less than s2 ");

else
printf("strings are equal ");

return 0;
}
int mystrncmp(const char *s1,const char *s2, int n)
{
while(*s1 && *s2)
{
if(*s1==*s2)
{
s1++;
s2++;
}
else
{
if(*s1<*s2)
{
return -1; //returning a negative value
}
else
{
return 1; //returning a positive value
}
}
}
return 0; //return 0 when strings are same
}

OUTPUT:

s1 is greater than s2

Process returned 0 (0x0) execution time : 0.030 s
Press any key to continue.

----------------------------------------------------------------------------------

2)

#include<stdlib.h>
#include<stdio.h>

char *mystrncpy(char* src,const char * tar,int n);
int main()
{
char source[20]="Amsterdam";
char *target = (char *)malloc(20 * sizeof(char));
target = mystrncpy(target,source,5);
printf("Our target is : %s",target);
return 0;
}
char *mystrncpy(char *dest, const char *src, int n)
{
int i=0;
char *dest1 = dest;
if(n > 20)
return NULL;

while(*src && i<n)
{
*dest1 = *src;
src++;
dest1++;
i++;
}
*dest1='';
return dest;
}

OUTPUT:

Our target is : Amste
Process returned 0 (0x0) execution time : 0.027 s
Press any key to continue.

----------------------------------------------------------------------------------------------------------------------------------------------------

3)

#include<stdlib.h>
#include<stdio.h>

int mystrrchr(const char* s,int c);
int main()
{
char source[20]="Amsterdam";
int comp;
comp = mystrrchr(source,'a');
if(comp == -1)
printf("character not found ");

else
printf("character found at position = %d ", comp);

return 0;
}
int mystrrchr(const char *s, int c)
{
int i=0;
//checking if character is there in string
while(*s != c && *s != '') {
s++;
i++;
}
//if character found then we return position otherwise -1
if(*s == c) {
return i;
}else {
return -1;
}
}

OUTPUT:

character found at position = 7

Process returned 0 (0x0) execution time : 0.033 s
Press any key to continue.