CProgram This code takes a product code eg ATL1203S14 or WSF
C-Program
This code takes a product code (e.g. ATL1203S14 or WSF1357S16) and separates the code into parts:
Warehouse: ATL
Product: 1203
Qualifiers: S14
I had it working, but now the output is all messed up, can anyone see the problem?
My Code:
#include <stdio.h>
 #include <string.h>
 #define BUFSIZ 10
 #define INPUT_SIZE 30
int main()
 {
 ///Declare Variables
 char buff[BUFSIZ], input[INPUT_SIZE];
 int first, last;
///Get data string representing product code
 printf(\"Enter MMOC Product Code: \");
 scanf(\"%s\", input);
/*Use loop to check the first position of the digit from the given input. Initially, start
 at position 0, then later, check the condition whether the first starting digit begins with any number between
 0-9. Increment the value of the first until it encounters with digit.*/
///Find Position of First Digit
 for(first = 0; !(input[first] >= \'0\' && input[first] <= \'9\'); ++first)
/*Copy the substring of input array value into a buff array with length first. Since, the value of first is
 obtained from the loop which is mentioned in the code above. Store the ending value as \"\\0\". Then, print
 the warehouse value from the given product code which is separated by using the method strncpy.*/
///Copying the string using strncpy()function
strncpy(buff, input, first);
 buff[first] = \'\\0\';
///Printing Warehouse
 printf(\"Warehouse: %s\ \", buff);
/*Use for loop to check the position of the product code from the given input. Initially, start the
 value of last which holds the value of the first, then later, check the condition whether the input
 starts with capital letter. Increment the value of last until it encounters with a capital letter.*/
///Finding First Capital Letters Following Digits
 for(last = first; input[last] < \'A\' || input[last] > \'Z\'; ++last)
/*Copy the substring of input array value into a buff array with length value (last-first). Store the ending
 value of buff as \"\\0\". Then, print the product value from the given product code which is separated by using
 the method strncpy. Then print the qualifiers value.*/
///Copying the string using strncpy()function
 strncpy(buff, &input[first], last - first);
 buff[last -first] = \'\\0\';
///Printing Product and Qualifiers
 printf(\"Product: %s\ \", buff);
 printf(\"Qualifiers: %s\ \", &input[last]);
///Return Successfully
return 0;
 }
Solution
Give it a check to the below code, it is running successfully in ideone:
#include <stdio.h>
 #include <string.h>
 #define BUFSIZ 10
 #define INPUT_SIZE 30
 int main()
 {
 ///Declare Variables
 char buff[BUFSIZ], input[INPUT_SIZE];
 int first, last;
 ///Get data string representing product code
 printf(\"Enter MMOC Product Code: \");
 scanf(\"%s\", input);
 /*Use loop to check the first position of the digit from the given input. Initially, start
 at position 0, then later, check the condition whether the first starting digit begins with any number between
 0-9. Increment the value of the first until it encounters with digit.*/
 ///Find Position of First Digit
 for(first = 0; !(input[first] >= \'0\' && input[first] <= \'9\'); ++first)
 /*Copy the substring of input array value into a buff array with length first. Since, the value of first is
 obtained from the loop which is mentioned in the code above. Store the ending value as \"\\0\". Then, print
 the warehouse value from the given product code which is separated by using the method strncpy.*/
 ///Copying the string using strncpy()function
 strncpy(buff, input, first);
 buff[first] = \'\\0\';
 ///Printing Warehouse
 printf(\"Warehouse: %s\ \", buff);
 /*Use for loop to check the position of the product code from the given input. Initially, start the
 value of last which holds the value of the first, then later, check the condition whether the input
 starts with capital letter. Increment the value of last until it encounters with a capital letter.*/
 ///Finding First Capital Letters Following Digits
 for(last = first; input[last] < \'A\' || input[last] > \'Z\'; ++last)
 /*Copy the substring of input array value into a buff array with length value (last-first). Store the ending
 value of buff as \"\\0\". Then, print the product value from the given product code which is separated by using
 the method strncpy. Then print the qualifiers value.*/
 ///Copying the string using strncpy()function
 strncpy(buff, &input[first], last - first);
 buff[last -first] = \'\\0\';
 ///Printing Product and Qualifiers
 printf(\"Product: %s\ \", buff);
 printf(\"Qualifiers: %s\ \", input);
 ///Return Successfully
 return 0;
 }
hope it helps.


