Tuesday 17 October 2017

C program to print details of the product manufactured during 2015

Pattern searching in a string can be done using the inbuilt function strstr() defined in string.h header file.

Question: The XYZ company manufactures various auto mobile spare parts suitable for 2,3 and 4 wheeler. To do this the company stores details of the products as sno, description, pcode and price. The product code is represented as alpha numeric code of four in length .The first two characters represents 2W,3W and 4W followed by a number to represent year of manufacture .Now write a program the details of the product manufactured during 2015.

For example: if pcode is 3W15 then the product belongs to 3 wheeler and it is manufactured in 2015.

Program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct product
{
            int sno;
            char description[200],pcode[5];
            int price;
};
void main()
{
            struct product p[4];
            int i;
            char *ptr;
            for(i=0;i<4;i++)
            {
                        printf("enter sno,desc,pcode,price of products");
                        scanf("%d%s%s%d",&p[i].sno,p[i].description,p[i].pcode,&p[i].price);
            }
            printf("The details of products manufactured in 2015 are\n")
            for(i=0;i<4;i++)
            {
                        ptr=strstr(p[i].pcode,"15");
                        if(ptr != NULL)
                        {
                                    printf("%d\t%s\t%s\t%d\n",p[i].sno,p[i].description,p[i].pcode,p[i].price);
                        }
            }
}

No comments:

Post a Comment