c program compare two strings code example

Example 1: statement o compare two strings in c

#include <stdio.h>
#include <string.h>

int main()
{
    char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
    int result;

    // comparing strings str1 and str2
    result = strcmp(str1, str2);
    printf("strcmp(str1, str2) = %d\n", result);

    // comparing strings str1 and str3
    result = strcmp(str1, str3);
    printf("strcmp(str1, str3) = %d\n", result);

    return 0;
}

Example 2: string compare in c

strcmp(str1, str2) = 32
strcmp(str1, str3) = 0

Example 3: string compare in c

strcmp(leftStr, rightStr);
//compares ascii value and gives positive, negative or zero.

Tags:

C Example