memcmp()Prototype: int memcmp(const void *buffer1, const void *buffer2, size_t count);Header File: string.h (C) or cstring (C++) Explanation: Alphabetically compares two arrays passed in to it. The fact that it is a void * simply means that it can have non-character arrays passed in to it. It is also necessary to pass how far to compare (ie, the size of the arrays) The return value is: Less than zero buffer1 is less than buffer2 Zero buffer1 is equal to buffer2 Greater than zero buffer1 is greater than buffer2Example:
//Program compares two character arrays
//Program compares only to eight letters
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
int comp_val=memcmp("Andersronc", "Zandimva", 8);
if ( comp_val == 0 )
{
cout<<"strings are equal";
}
if ( comp_val<0 )
{
cout<<"String one is alphabetically equal";
}
else
{
cout<<"String one is alphabetically greater";
}
}
Other Functions
|