Use strcmp() function to compare string - C String

C examples for String:String Function

Description

Use strcmp() function to compare string

Demo Code

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

int main( void )
{
    char str1[80], str2[80];
    int x;/*from   www  .  j  a  v a2s  .c o m*/

    while (1)
    {
        printf("\n\nInput the first string, a blank to exit: ");
        gets_s(str1);

        if ( strlen(str1) == 0 )
            break;

        printf("\nInput the second string: ");
        gets_s(str2);

        /* Compare them and display the result. */

        x = strcmp(str1, str2);

        printf("\nstrcmp(%s,%s) returns %d", str1, str2, x);
    }
    return 0;
}

Result


Related Tutorials