Write a C program to compare two strings character by character.Read the string using character pointer.Pass the character pointer to a function "compare" and print equal/not equal.Sample InputcatCatSample Outputnot equal
Question
Write a C program to compare two strings character by character.Read the string using character pointer.Pass the character pointer to a function "compare" and print equal/not equal.Sample InputcatCatSample Outputnot equal
Solution
Sure, here is a simple C program that compares two strings character by character:
#include <stdio.h>
void compare(char *str1, char *str2) {
while(*str1 && *str2) {
if(*str1 != *str2) {
printf("not equal\n");
return;
}
str1++;
str2++;
}
if(*str1 || *str2) {
printf("not equal\n");
} else {
printf("equal\n");
}
}
int main() {
char str1[100], str2[100];
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);
compare(str1, str2);
return 0;
}
This program first reads two strings from the user. It then passes these strings to the compare function. This function compares the strings character by character. If it finds a mismatch, it immediately prints "not equal" and returns. If it reaches the end of both strings without finding a mismatch, it prints "equal". If it reaches the end of one string but not the other, it also prints "not equal", because the strings are of different lengths.
Similar Questions
What is the function to compare two strings lexicographically in C programming?
#include <stdio.h> int main() { char *str = "hello, world"; char *str1 = "hello, world"; if (strcmp(str, str1)) printf("equal"); else printf("unequal"); } equalunequalCompilation error Depends on the compiler
The program needs to compare two given strings lexicographically and output the difference between them. (using compareTo() function)If the first string is greater than the second string, it should output a positive value, If they are equal it should output 0If the first string is less than the second string, it should output a negative valueInput format :The first line of the input consists of a string.The second line of the input consists of a string.Output format :The output should display the following constraintsNote :If (string1 > string2), it returns a positive value (the difference between the characters).If both the strings are equal lexicographically, i.e., (string1 == string2), it returns 0.If (string1 < string2), it returns a negative value (the difference between the characters).Sample test cases :Input 1 :harryharryOutput 1 :0Input 2 :helloworldOutput 2 :-15Input 3 :tiger lionOutput 3 :8
Develop a Java program with a method for comparing two strings.
What is the output of the following code?char str1[] = "abc";char str2[] = "abc";if (str1 == str2) printf("Equal\n");else printf("Not Equal\n");
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.