Article ID: 138206
Article Last Modified on 12/11/1999
#include <stdio.h>
#include <string.h>
int mycompare( char *, char * ); /* declare compare function */
main()
{
char a1[]="Test", a2[]=""; /* data to compare */
/* Call the compare function */
printf("%c\n", mycompare(a1,a2)?'T':'F' );
/* Call the compare function, switching the addresses of the data */
printf("%c\n", mycompare(a2,a1)?'T':'F' );
} /* end of main program */
mycompare( char * c1, char * c2)
{
/* This is an example of C source code to compare two 'strings' */
/* in memory. The starting addresses of the two strings already */
/* in memory is passed to this function and assigned to local */
/* variables designated by "star" (meaning 'pointer to' or */
/* 'address of') c1 and "star" c2 */
/* Create a local variable to hold the length of the string, loop */
/* counter, and the result */
int iLen, iCounter, iResult;
/* get the size of the string being compared to the primary string*/
iLen = strlen( c2 );
/* initially set the result to true (non-zero) */
iResult = 1;
for ( iCounter=0; iCounter < iLen; iCounter++)
/* A standard C 'for' loop. The first element of any C array is */
/* element zero, not element one, so you start the counter at zero.*/
/* Now, the array in memory actually contains the 'string' plus */
/* an additional ASCII zero terminating character. You must */
/* subtract one from the length to get the actual length of the */
/* string being compared, thus iLen-1. Even so, because the loop */
/* counter begins at zero and the strlen function returns the */
/* actual length of the string, you loop as long as the counter */
/* is less than the length. */
{
if ( *(c1+iCounter) != *(c2+iCounter) )
if (iResult)
iResult = 0;
}
/* This code goes through and compares both strings character */
/* by character until the end of string 2, pointed to by */
/* *(c2+iCounter, is reached. During this time, if the characters */
/* do not match and the result is not already false, the result */
/* is set to false. */
/* But what happens if iLen-1 is already zero because the string */
/* is empty? You never enter the loop and iResult remains set */
/* to 1 (true). This is returned by the function, thus */
/* "Test"="" is true. However, when you reverse the data, iLen-1 */
/* contains the length of the string being compared. You enter */
/* the For loop and iResult is set to false when comparing the */
/* first character. */
return iResult;
} /* end of mycompare() function */
Keywords: kbhowto kbcode KB138206