Algorithm
- We use
string.h
header library of C. - Then do comparison using
strcmp(string1,string2)
.- If Difference = 0, then matched -
strcmp(string1,string2) == 0
- Else Not matched
- If Difference = 0, then matched -
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <string.h>
int main()
{
char string1[100], string2[100];
printf("Enter string 1: \n");
scanf("%s", string1);
printf("Enter string 2: \n");
scanf("%s", string2);
if (strcmp(string1,string2) == 0) {
printf("Strings are equal.\n");
} else {
printf("Strings are not equal.\n");
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Enter string 1: Akash
Enter string 2: Akash
Enter string 2: Akash
Output
The strings are equal.
Demonstration
This is a very simple example of String comparison in C programming. We've used string.h
header library of C programming.
Then just use the strcmp()
function to comparison.
Run C Programming Code Online - https://devsenv.com/page/run-c-programming
For more example of string.h
header library, check from here - https://www.cplusplus.com/reference/cstring