Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to take a product code from Millie\'s Mail-Order Catalog (MMOC)

ID: 3920552 • Letter: W

Question

Write a program to take a product code from Millie's Mail-Order Catalog (MMOC) and separate it into its component parts. An MMOC product code begins with three letters identifying the warehouse where the product is stored. Next come four digits that are the product ID. The final field of the string starts with a capital letter and represents qualifiers such as size, color, and so on. For example, ATL1203S14 stands for product 1203, size 14, in the Atlanta warehouse. Write a program that takes a code from a, finds the position of the first digit and of the first letter after the digits, and uses strcpy and strncpy to display a report such as the following: Warehouse: ATL Product: 1203 Qualifiers: S14

Explanation / Answer

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 10

int get_data(char *);

int main()
{
    char product_code[MAX];
    char warehouse[3];
    char product[4];
    char qualifiers[3];

    printf("Please Enter Product code ");
        /* Accept input and check for validation*/
    if( get_string(product_code)) {

        strncpy(warehouse, product_code, 3);
        warehouse[3]='';    /*Assign Null character after copy string*/

        strncpy(product, product_code+3, 4); /* we need to start from 4th character*/
        product[4]='';

        strncpy(qualifiers, product_code+7, 3);
        qualifiers[3]='';

        printf("Warehouse : %s ",warehouse);
        printf("Product : %s ",product);
        printf("qualifiers : %s ",qualifiers);
    }
    else{
        printf("Product Code Can't Accept ");
    }
    return 0;
}

int get_string(char* product_code)
{
    int i;
    scanf("%s", product_code);

    for(i=0; i<10; i++){
        if(i <= 2){
            if(! isalpha(product_code[i])){   /* validate character*/
                return 0;
            }
        }
        if( i >=3 && i<=6){
            if(! isdigit(product_code[i])){   /*validate digit*/
                return 0;
            }
        }
        if( i ==7 ){
            if(! isalpha(product_code[i])){  
                return 0;
            }
        }
        if( i >=8 && i<=9 ){
            if(! isdigit(product_code[i])){
            return 0;
            }
        }
    }
    return 1;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote