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

Write a function called stringLen which accepts two arguments. Each argument con

ID: 3852024 • Letter: W

Question

Write a function called stringLen which accepts two arguments. Each argument contains the address of the first character string. Your function must return the length of one of those strings if the strings pointed to by the two arguments are exactly the same length and contain exactly the same characters, -1 otherwise. Examples: "hello" "hello" Return 5 because the lengths and all characters are the same "hello world" "hello"Return-1 because the lengths are different "xyzyx" "xyyyx" Return -1 because a character in the two equal-length strings is different

Explanation / Answer

#include <stdio.h>
int stringLen(char name1[], char name2[]);
int main(){
char name1[100], name2[100];
printf("Enter your name1 : ");
fgets(name1, 100, stdin);
printf("Enter your name2 : ");
fgets(name2, 100, stdin);
  
printf("%d",stringLen(name1, name2));
  
return 0;
}

int stringLen(char name1[], char name2[]){
  
int i, flag;
i = 0;
flag = 0;
while (name1[i] == name2[i] && name1[i] != '' && name2[i] != '' ){
i++;
if (name1[i] > name2[i]){
flag = 1;
break;
}else if (name1[i] < name2[i]){
flag = 1;
break;
}else{
continue;
}
}
if(flag != 1){
return i;
}else{
return -1;
}
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote