strcmp - C string.h

C examples for string.h:strcmp

Type

function

From

<cstring> 
<string.h>

Description

Compares the C string str1 to the C string str2, letter by letter.

Prototype

int strcmp ( const char * str1, const char * str2 );

Parameters

Parameter Description
str1 C string to be compared.
str2 C string to be compared.

Return Value

Returns an integral value indicating the relationship between the strings:

return value indicates
<0 ptr1 has lower value than ptr2
0 the contents of both strings are equal
>0 ptr1 has higher value than ptr2

Demo Code

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

int main ()/*w ww  . j a  v  a  2s.  com*/
{
  char key[] = "apple";
  char buffer[80];
  do {
     printf ("Guess my favorite fruit(hint:apple)? ");
     fflush (stdout);
     scanf ("%79s",buffer);
  } while (strcmp (key,buffer) != 0);
  puts ("Correct answer!");
  return 0;
}

Related Tutorials