Home Library Index Site Map  Links Search About

Utility functions


 
 
 
 

LexCmp()
// Function LexCmp() for comparing two arrays
#include <iostream.h>
#include <conio.h>
int LexCmp(char* , char*);

void main()
{
do
{
char a[30], b[30];
cout<<"\nEnter first string "; cin>>a;
cout<<"\nEnter second string "; cin>>b;
cout<<LexCmp(a, b);
cout<<'\n'<<"Press y to continue any key to exit...";
}while(getch()=='y');
}

// int LexCmp(char*,char*)
// returns: 0 if the same, -1 if a smaller, 1 if a bigger
//----------------------------
int LexCmp(char* a, char*b)
{
int i=0;
while(a[i] && b[i])
{
if(a[i] > b[i]) return 1;
if(a[i] < b[i]) return -1;
else i++;
}
if(!a[i]&& b[i]) return -1;
if(!b[i]&& a[i]) return 1;
return 0;
}
LexCmp compares the values of two strings. It is assumed that
case is not relevant.
Home Library Index Top

 

Cmp()
// A general comparison template function
#include <iostream.h>


template<class A, class B>
int Cmp(A a, B b)
{
if(a>b) return 1;
if(a<b) return -1;
else return 0;
};
Cmp compares two types for which the < and > operators must be defined
Home Library Index Top

STL Database
Arrays
Binary File Input / Output
Character Input
Containers
Standard Template Library
Streams
Templates
Utility Functions
Win32 Programming
Miscellaneous
 

 
copyright notice

Copyright Robert Mitchell. Last Revised : 28 April, 2000

e-mail me
Hosted by www.Geocities.ws

1