1// use: strcmp(string1, string2);
2
3string a = "words";
4string b = "words";
5
6if (strcmp(a, b) == 0)
7{
8 printf("a and b match");
9 // strcmp returns 0 if both strings match
10}
11
12else
13{
14 printf("a and b don't match");
15 // strcmp returns anything else if the strings dont match
16}
1#include <stdio.h>
2#include <string.h>
3int main(int argc, char const *argv[]) {
4 char string1[] = {"tutorials point"};
5 char string2[] = {"tutorials point"};
6 //using function strcmp() to compare the two strings
7 if (strcmp(string1, string2) == 0)
8 printf("Yes 2 strings are same\n");
9 else
10 printf("No, 2 strings are not same\n" );
11 return 0;
12}
1// strCmp implementation
2// string1 < string2 => return a negative integer
3// string1 > string2 => return a positive integer
4// string1 = string2 => return 0
5int strCmp(const char* s1, const char* s2) {
6 while(*s1 && (*s1 == *s2)) {
7 s1++;
8 s2++;
9 }
10 return *s1 - *s2;
11}