[LCP]String comparison.
David Filion
filiond at videotron.ca
Thu Feb 21 04:58:10 UTC 2002
OK, I need to compare the contents of two text files. I don't want
to use diff because it does not provide output in the format I
would like. (OK, I could pass the output through other utilities like
sed, awk or perl but that's too easy.) As a result I thought I would
try writing something myself.
Like diff, I want to see if line 1 if file A matches a line
in file B.
Now, on technique would be to just go line by line doing a
strcmp(), but that's no fun. Another technique I can across
was generating a modulus for the string being searched for and
looking for lines in the second file with the same modulus value
and only comparing those lines for an exact match.
While trying this I came across a problem. The lines are 256
character long (letter, digits, special characters but no
control codes). With a modulus of 11 this results in hugh
numbers and trouble some results. I could just keep upping
the modulus from 11 to higher numbers until I receive
reasonable results but I not sure this is the right
thing to do.
I've included some sample code below with a sample line of
data that I trying to calculate the modulus for.
Please let me know how I could improve things. If all I need
to do is tweak the syntax great. If you have a better suggestion
even better.
Thanks in advance.
ps. This is NOT homework. I'm just a programmer with WAY
too much time on his hands.
============== Start of code ================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
strmod (const char* str, const int modulus)
{
register size_t pos = 0; /* Position in str */
size_t len = 0; /* Lenght of str (less terminating NULL) */
unsigned long int sum = 0; /* Sum of weighted digits. */
int remainder= 0; /* Remainder of sum / modulus */
int check_digit = 0; /* Generated check digit. */
/* make sure there is something to work with. */
if (!str || (modulus == 0))
return (-1);
printf ("Modulus number is: %d\n", modulus);
len = strlen (str);
for (pos = 0; pos < len; pos++) {
printf ("%c = %d\n", str[pos], (int)str[pos] - 32);
sum += ((int)str[pos]) - 32;
}
printf ("Sum of weighted characters: %d\n", sum);
printf ("Field/modulus number: %f\n", (float)sum/modulus);
remainder = sum % modulus;
check_digit = modulus - remainder;
printf ("Remainder is: %d\n", remainder);
printf ("Check digit = %d-%d = %d\n", modulus, remainder,
check_digit);
return (check_digit);
}
int
main (int ac, char **av)
{
/* Big ass string */
char *string2 =
"0000F00103A1|81510001||||2001/03/15|2001/06/15|3|-2|0|0|1|MA-AD0|MINGARD|103584358|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|000252948002||363618|1|MONTREAL
-
H1L||DANIELLE
|";
int modulus = 43;
int check_digit = 0;
printf ("Original string: %s\n", string2);
check_digit = strmod (string2, modulus);
printf ("New string: %s%d\n", string2, check_digit);
exit (EXIT_SUCCESS);
}
=============== End of code ================================
More information about the linuxCprogramming
mailing list