首页 > 解决方案 > split a string into letters and digits in c

问题描述

I want to write a code to split the numbers and characters in this pattern by finding finding the position of first digit and alphabet and using strcpy and strncpy in c

input:

ATL1203S14

output:

warehouse: ATL

product: 1203

Qualifiers: S14

The warehouse has only alphabet and product has only number but qualifiers starts with a capital letter and its following digits. but the input can vary in the number of digits and alphabets.

so basically i wanted to flag the first character and digit in the input and then copy them into a new string but it doesn't give me the warehouse output and also i didn't have any idea on how to identify the Qualifiers.

so I would be very thankful and happy if anybody can help me with the code.

#include <stdio.h>
#include <string.h>

#define MMOC_len 20

int main()
{
    char MMOC_cod[MMOC_len];
    char warehouse[MMOC_len];
    char productNo[MMOC_len];
    char qualifiers[MMOC_len];
    int current, next, Ch_ind, No_ind;

    printf("Hello World enter your MMOC like >ATL1203S14< \n");
    scanf("%s", MMOC_cod);

    for (current=0; current<= strlen(MMOC_cod); current++)
    {
        if(MMOC_cod[current] >= '0' && MMOC_cod[current] <='9')
        {
            No_ind= current;
            break;
        }

        else if(MMOC_cod[current] >= 'A' && MMOC_cod[current] <='Z')
        {
            Ch_ind= current;
            break;
        }
    }
            strncpy(productNo, &MMOC_cod[current], (Ch_ind - No_ind-1));
            productNo[No_ind-1]= '\0';
            printf("product Number: %s", productNo);

            /// the warehouse part doesn't work also
            /// I cant set condition for Qualifiers as well but I cant find the problem
            strncpy(warehouse, &MMOC_cod[0], (No_ind-1));
            warehouse[No_ind-Ch_ind]= '\0';
            printf("warehouse: %s", warehouse);


    return 0;
}

标签: cstringsplit

解决方案


You are making things difficult on yourself. This is a circumstance where a well-crafted format-string for sscanf() can separate the input into the values for warehouse, product and qualifiers you need, e.g.

#include <stdio.h>

#define SUBSTRSZ 32         /* if you need a constant, #define one (or more) */

int main (void) {
    
    char *input = "ATL1203S14";                         /* input */
    char warehouse[SUBSTRSZ], qualifiers[SUBSTRSZ];     /* string storage */
    unsigned product;                                   /* treat product as unsigned */
    
    /* separate input into warehouse, product & qualifiers, VALIDATE the conversion */
    if (sscanf (input, " %31[^0-9] %u %31[^\n]", warehouse, &product, qualifiers) != 3) {
        fputs ("error: invalid input format.\n", stderr);
        return 1;
    }
    
    printf ("warehouse: %s\nproduct: %u\nQualifiers: %s\n",
            warehouse, product, qualifiers);
}

The format-string " %31[^0-9] %u %31[^\n]":

  • " " discards leading whitespace, if any, because "%c" and "%[..]" do not do it on their own,
  • %31[^0-9] reads up to a max of 31-chars (preserving room for '\0' protecting your array bounds) that are not digits,
  • %u unsigned conversion to handle up to 10-digits (adjust type to uint64_t if more digits needed or to uint32_t if unsigned less than 32 bits on your system), You can also change product to a string and use use %31[0-9] if you prefer handling it that way,
  • %31[^\n] read remainder of line up to max of 31 chars.

Example Use/Output

$ ./bin/splitcode
warehouse: ATL
product: 1203
Qualifiers: S14

Let me know if you have further questions.


推荐阅读