codescracker


c

C strcmp()



« Previous Chapter Next Function »


strcmp()

The strcmp() function lexicographically compares two strings and returns an integer based on the outcome as shows here in the following table:

Value Meaning
Less than zero str1 is less than str2
Zero str1 is equal to str2
Greater than zero str1 is greater than str2

strcmp() Syntax

Following is the syntax of the strcmp() function :

#include<string.h>
int strcmp(const char *str1, const char *str2);

strcmp() Example

You can use the following function as a password-verification routine. It returns zero on failure and 1 on success :

int password(void)
{
	char s[80];
	printf("Enter password: ");
	gets(s);
	if(strcmp(s, "pass"))
	{
		printf("Invalid Password\n");
		return 0;
	}
	return 1;
}

« Previous Chapter Next Function »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test