someone please help me with my C programing assignment Write a function, named m
ID: 3821622 • Letter: S
Question
someone please help me with my C programing assignment
Write a function, named my_strcmp, that receives two strings and performs a string comparison. Assuming strings A and B of the same length, the function will return the following values: -1 if A precedes B alphabetically 0 if A is the same string as B 1 if A follows B alphabetically Since a string in C is a character array, your function must be written to receive two arrays (one for each string). The difference between the array in Problem #2 and this problem is that there is no need to give the function the array size because you can search for the null terminator '' or use strlen. For simplicity, assume each string will be single word that is the same size, for example, "car" and "cat" (returns -1), or "trash" and "speed" (returns 1)Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int my_strcmp(char string1[],char string2[])
{
int length=strlen(string1);
int i;
for(i=0;i<length;i++)
{
if(string1[i]==string2[i])
;
else if(string1[i]<string2[i])
{
return -1;
}
else
return 1;
}
return 0;
}
int main()
{
char string1[]="car";
char string2[]="cat";
int output=my_strcmp(string1,string2);
printf("output=%d ",output);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.