Read the data from file 'cities.txt' containing names of their STD codes. Accept a name of city from user and use linear search algorithm to check whether the name is present in file and output the STD code, otherwise output 'city not in list'.

 


//Reading from file

#include<stdio.h>



typedef struct city

{

    char name[20];

    int code;

} city;



//Fileread

int fileread(city a[20])

{

    FILE *fp;

    int i=0;



    fp=fopen("cities.txt","r");

    if(fp==NULL)

        printf("File Not Exist");

    else

    {

        while(!feof(fp))

        {

            fscanf(fp,"%s%d", a[i].name, &a[i].code);

            i++;

        }

        fclose(fp);

    }

    return i-1;

}



//Main

int main()

{

    int i, n;

    char key[20];

    city a[20];

    n = fileread(a);



    for(int i=0; i<n; i++)

        printf("%s %d\n", a[i].name, a[i].code);

       /* displaying records*/



linearsearch(n);



}

//Linear Search

linearsearch(int n)

{

city a[20];

    n=fileread(a);

  char str[20];

        int index,flag=0;

        printf("Enter city:");

        scanf("%s",str);

        for(int i=0;i<n;i++)

        {

            if(strcmp(str,a[i].name)==0)

            {

                flag=1;

                index=i;

            }

        }

        if(flag==1)

            printf("City Code: %d",a[index].code);

            else

            printf("City Not in list");

}

Post a Comment

3 Comments

Thanks,To visit this blog.