Need help below, need to write the code in main of what is being asked, its alre
ID: 3540126 • Letter: N
Question
Need help below, need to write the code in main of what is being asked, its already been started see below, Thanks in advance!
int compare ( const char *p, const char *q) {
while (*p != '' && *p == *q {
p++;
q++;
}
/* compare the first n characters of two c strings.
Pre
p - a pointer to the first c string
q - a pointer to the second c string
n- a positive integer
Post
returns 0 if two strings are equal
a negative number if the first is less than second
a positive number if the first is greater than second
*/
int compareN (const char *p, const char *q, int n) {
int counter = 0;
Explanation / Answer
#include<stdio.h>
#include<conio.h>
int compareN (const char *p, const char *q, int n) ;
int main(){
int n;
char str1[50];
char str2[50];
printf("Enter first string=");
fgets(str1,50,stdin);
printf("Enter second string=");
fgets(str2,50,stdin);
printf("Enter the number You want to compare=");
scanf("%d",&n);
if(!compareN(str1,str2,n))printf("equal");
else printf("not equal");
getch();
return 0;
}
int compareN (const char *p, const char *q, int n){
int counter=0;
while ((*p != '' ) &&(*q != '' )&& (*p == *q)&& (counter<n)) {
p++;
q++;
counter++;
}
if(counter==n) return 0;
else if(*p>*q) return *p-*q;
else return *q-*p;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.