C strcasecmp function ?
by Michael Cozzolino · in Technical Issues · 02/23/2001 (11:39 am) · 4 replies
Ok I am having trouble with a function I need to do for a project.
Can somebody explain how to set this up using the strcasecmp. User enters last name then first name. The string is compared to the names read in from a file. I sort thru the array and if found display if not give an error.
Can somebody explain how to set this up using the strcasecmp. User enters last name then first name. The string is compared to the names read in from a file. I sort thru the array and if found display if not give an error.
About the author
Indie Developer in the Albany NY area. iOS, PC, Mac OSX development. http://itunes.apple.com/us/artist/michael-cozzolino/id367780489
#2
02/25/2001 (7:30 pm)
I am using C. An example would be good. I don't think I need the whole sha-bang. Thanks. So far my teacher has not demonstrated it. I don't particullarly like my teachers style; she has me spinning my wheels for two weeks before she just plain out teaches something we are all struggling with. And our text sucks as far as examples go.
#3
--Rick
Several Notes:
-the list is always empty in this example, so it will never find a match. I wanted to keep this example as short as possible. I leave it as an exercise to insert the name into the list.
-I used strcmp(). You change it to what ever case compare function your compiler supports, stricmp, strcmpi, strcasecmp, etc, etc. FYI string case comparison is NOT a POSIX api so it will be different on most compilers. For more information about POSIX programming I highly recommend this book the POSIX Programmers Guide. There is a small stack of books that never leave my desk this is one of them.
-This is a brute force way to accomplish this task. If I needed to do something like this in production code I would probably use a hash.
-I used fgets() rather than gets() so that I could specify the maximum string length and not have to worry about buffer overruns.
02/25/2001 (10:46 pm)
ok, here is a very simple chunk of example code that inputs the first and last and last name and compares it to a list. Hope this helps,--Rick
Several Notes:
-the list is always empty in this example, so it will never find a match. I wanted to keep this example as short as possible. I leave it as an exercise to insert the name into the list.
-I used strcmp(). You change it to what ever case compare function your compiler supports, stricmp, strcmpi, strcasecmp, etc, etc. FYI string case comparison is NOT a POSIX api so it will be different on most compilers. For more information about POSIX programming I highly recommend this book the POSIX Programmers Guide. There is a small stack of books that never leave my desk this is one of them.
-This is a brute force way to accomplish this task. If I needed to do something like this in production code I would probably use a hash.
-I used fgets() rather than gets() so that I could specify the maximum string length and not have to worry about buffer overruns.
#include <stdio.h>
#include <string.h>
#define MAX_FIRST 20
#define MAX_LAST 40
#define MAX_LIST 100
typedef struct
{
char first[MAX_FIRST];
char last[MAX_LAST];
}Person;
static Person List[MAX_LIST];
static int ListIndex = 0;
int main( void )
{
int i;
Person p;
printf("Enter First Name (max %d characters): ", MAX_FIRST-1);
fgets(p.first, MAX_FIRST-1, stdin);
printf("\nEnter Last Name (max %d characters): ", MAX_LAST-1);
fgets(p.last, MAX_LAST-1, stdin);
for(i=0; i < ListIndex; i++)
{
if (strcmp(List[i].first, p.first) == 0 &&
strcmp(List[i].last, p.last) == 0)
{
printf("Found a Match");
return 0;
}
}
printf("Did not find a match");
return 0;
}
#4
void search(char *names[], float *scores[], int totalstudents)
{ /*start search function */
int c,
r,
found =0;
char tempname [NAMELENGTH];
char last[12];
char first[12];
char *lastname;
char *firstname;
printf("\n\n");
printf("Enter the student's lastname. \n\n");
fflush(stdin);
gets(last);
printf("Enter the student's firstname. \n\n");
fflush(stdin);
gets(first);
for(r=0; r<= totalstudents-1; r++){
strcpy(tempname, names [r]);
lastname =strtok(tempname, ",");
firstname =strtok(NULL,NULL);
if(strcasecmp(last,lastname)==0 && strcasecmp(first, firstname) ==0){
printf("\n\n");
printf("%-12s%-11s%9s%8s%8s%8s%7s%10s\n", "Lastname","Firstname",
"Exam 1","Exam 2", "Exam 3", "Exam 4", "Final", "Average");
printf("-------------------------------------------------------------------\
-------\n");
printf("%-12s%-11s",last,first);
for(c=0; c<=MAXGRADES-1; c++)
printf("%8.1f", *(scores[r]+c));
printf("\n");
found =1;
break;
}
}
if(found ==0){
printf("\n\n");
printf("******Student is not on file******\n\n");
}
}
02/28/2001 (12:03 pm)
Ok I just finished this so all the documentation isn't here for anyone using this as a reference. :) Rick, your example helped me with the thought process but unfortunately we haven't learned about struct's yet or fgets. Here is how I had to do it. Thanks for the help. Here is another example if someone gets into the same jam and has limited knowledge like me ie. not knowing struct's.void search(char *names[], float *scores[], int totalstudents)
{ /*start search function */
int c,
r,
found =0;
char tempname [NAMELENGTH];
char last[12];
char first[12];
char *lastname;
char *firstname;
printf("\n\n");
printf("Enter the student's lastname. \n\n");
fflush(stdin);
gets(last);
printf("Enter the student's firstname. \n\n");
fflush(stdin);
gets(first);
for(r=0; r<= totalstudents-1; r++){
strcpy(tempname, names [r]);
lastname =strtok(tempname, ",");
firstname =strtok(NULL,NULL);
if(strcasecmp(last,lastname)==0 && strcasecmp(first, firstname) ==0){
printf("\n\n");
printf("%-12s%-11s%9s%8s%8s%8s%7s%10s\n", "Lastname","Firstname",
"Exam 1","Exam 2", "Exam 3", "Exam 4", "Final", "Average");
printf("-------------------------------------------------------------------\
-------\n");
printf("%-12s%-11s",last,first);
for(c=0; c<=MAXGRADES-1; c++)
printf("%8.1f", *(scores[r]+c));
printf("\n");
found =1;
break;
}
}
if(found ==0){
printf("\n\n");
printf("******Student is not on file******\n\n");
}
}
Torque Owner Rick Overman