NEW CONTEST 
Author Message
 NEW CONTEST

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;

Quote:
}

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;

Quote:
}

--
| A.Champion                |


Mon, 03 Aug 1998 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. NEW CONTEST!

2. NEW CONTEST!

3. New Contest! (pointer)

4. CONCISENESS CONTEST ROUND IV -- tokount.c (new)

5. CONCISENESS CONTEST CLARIFICATION - new tokount.c

6. C Contest: previous contest winners?

7. repost 1991 C contest rules - contest ends 16-May-91 0:00 UTC

8. Programming Contest: Contest Clarification

9. New New New

10. kyduke(kyduke@yahoo.com)'s New Logic - New sorting, New paging method

11. New! UTexas IEEECS Nat. Programming Contest Application update!!

12. Ideas for new project- new to C++

 

 
Powered by phpBB® Forum Software