Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Need help in comparing string of array in C

Posted on 2003-03-17
3
Medium Priority
?
222 Views
Last Modified: 2010-04-15
This problem should be simple but I just forgot the easier way to handle it. Consider the following program:

=======================================================================================
// dbfile.txt - the text file simulating fairly simple database

101 chan 1800 sales 1998
102 john 4000 sales 2000
103 myke 6000 sales 2001
104 sarah 2800 sales 1994
105 ali 3000 it_dept 1996

=======================================================================================
// source file

#include<stdio.h>
#include<malloc.h>

FILE *dbfile;

struct emp
{
     int id;
     char name[10];
     int salary;
     char dept[10];
     int yearoj;
     struct emp *next;
};

struct emp *temp;
struct emp *list;

void main()
{  
     int j;
     dbfile = fopen("dbfile.txt", "r");
         
     list=NULL;
     for(j=0; j<5; j++)
     {
          temp=(struct emp*) malloc(sizeof(struct emp));
          fscanf(dbfile, "%d", &temp->id);
          fscanf(dbfile, "%s", temp->name);
          fscanf(dbfile, "%d", &temp->salary);
          fscanf(dbfile, "%s", temp->dept);
          fscanf(dbfile, "%d", &temp->yearoj);
          temp->next=NULL;

          if(list==NULL)
               list=temp;
          else
          {
               temp->next=list;
               list=temp;
          }

     }

     temp=list;
     printf("\nThe ID no. of employees who get < 3000 in the sales dept. who join before 1995:\n");
     while(temp!=NULL)
     {
          if(temp->salary < 3000 && temp->dept == "sales" && temp->yearoj < 1995)
               printf("%d\n", temp->id);
          temp=temp->next;
     }

     fclose(dbfile);
}
==========================================================================

There should be an entry found. However, the program can't find any matched entry due to the temp->dept == "sales". I believe I can't match the string like this. May I know in C, how to compare the string?

Thanks and hope to hear from you soon.
0
Comment
Question by:cwchan80
3 Comments
 
LVL 6

Expert Comment

by:zebada
ID: 8150630
if(temp->salary < 3000 && strcmp(temp->dept,"sales")==0 && temp->yearoj < 1995)
 
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 80 total points
ID: 8150711
>> if(temp->salary < 3000 && temp->dept == "sales" && temp->yearoj < 1995)
   
Make it:

if(temp->salary < 3000 && ( strcmpi (temp->dept, "sales") == 0 ) && temp->yearoj < 1995)
   
(strcmpi () is for case-insensitive comparision of strings and returns 0 if the strings are same, returns greater than 0 if the first string should be alphabetically after the second in dictionary order, else returns a negative value).

You also need to include <string.h> for that (and for strcmp () also, which does the same thing but is case-sensitive).

However, there might still be a problem in printing the correct ID. Please run it and let us know...

Mayank.
0
 

Author Comment

by:cwchan80
ID: 8150916
Thanks zebada and mayank. The solution works. The ID can be printed correctly.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question