C language Your task for this activity is to implement the following functions:
ID: 3852863 • Letter: C
Question
C language
Your task for this activity is to implement the following functions:
e. checkGuess():
This function should take two strings as input. If the two strings are equivalent, return a 1 from the function. If they’re different, return a 0. There are at least two ways to do this: you may use the strcmp() function from <string.h>, or you can iterate over every character in the strings. You may assume that the strings are equal length.
Given:
#include<stdio.h>
#include<string.h>
// Function prototype should be placed below
int main(void){
//TODO: compare two strings
char s1[10];
char s2[10];
printf("Enter the first string: ");
scanf("%s", &s1);
printf("Enter the second string: ");
scanf("%s", &s2);
int c = checkGuess(s1, s2);
// output if they are matched or not
return 0;
}
/* Complete the function */
checkGuess(){
}
Explanation / Answer
Since we are assuming that both the strings are of equal length, so our checkGuess() code will be:
int checkGuess(char str1[10], char str2[10]) // passing strings 1 and 2 as str1 and str2 to checkGuess function
{
int i=0;
while (str1[i]==str2[i] && str1[i]!='') // checking by iteration method wheather both strings are equal or not
i++;
if (str1[i] > str2[i])
printf(" String 1 is greater than String 2");
return (0);
else if (str1[i] < str2[i])
printf("String 2 is greater than String 1");
return (0);
else
printf("String 1 is equal to String 2");
return (1);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.