Article View: comp.lang.c.moderated
Article #1999NEW CONTEST
From: Alun Champion
Date: Thu, 15 Feb 1996 15:47
Date: Thu, 15 Feb 1996 15:47
72 lines
2582 bytes
2582 bytes
2. Write a function which compares two strings, ignoring case. You should
use the islower() and toupper() commands. Write a small program which uses
this function to compare its arguments.
Here's a simple attempt ];')
/*
* This program compares strings in a case insensitive way
* I have used pointers throughout as they are faster and demonstrate I
* a greater understanding of the language than others in my class
*
* (C) Alun Champion - make sure noone steals it from me.
*/
#include <stdio.h>
/* Using global variable so I can do lots of comparisons to the same string */
char* s;
/* Tells the search algorithm that "s" is a new parameter - if it is not a
new parameter the search algorithm can do some optimisations from what it
learnt from meeting it before - clever !! */
int new;
int
StrCaseCmp(char* s1)
{
/* Using a sum so that if the user of the function wants to he/she can
examine the ordinance of the difference between the strings */
int sum = 0;
char* s2 = s;
while (*s2) {
/* We can perform optimisations here if we have met the string before,
we don't need to check if it is an uppercase character the
algorithm just remembers that this is the case */
if (new && (*s2 = toupper(*s2)));
sum += *s1 - *s2++ + 'A' - (islower(*s1) ? 'a' : 'A');
s1++;
}
/* We have met this comparison criteria now - prepare to do optimisations
next time - neat !! */
new = 0;
return sum;
}
int
main(void)
{
char a[] = "Hello World";
char *l[] = { "heLLo wORld", "hell word", "HELLO WORLD", "" };
char **list = l;
/* Comparing against a new parameter */
new = 1;
s = a;
printf("Comparing against: %s\n", a);
do {
/* For this example I am not examing the ordinance of the difference
between the strings - though the function allows me to do this */
printf("%s is %s\n", *list, StrCaseCmp(*list) ? "Not Equal" : "Equal");
/* Everyone else uses *ptr++ - I can use **++ptr, demonstrating my
in depth understanding of pointers */
} while (**++list);
return 0;
}
--
| A.Champion |
Technical Details
Message-ID:
<4fvkhs$euv@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: Alun.Champion@bridge.bst.bls.com (Alun Champion) Newsgroups: comp.lang.c.moderated Subject: NEW CONTEST Date: 15 Feb 1996 09:47:08 -0600 Organization: Computer People Inc. Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4fvkhs$euv@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com