c - 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;
}
解决方案
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 touint64_t
if more digits needed or touint32_t
ifunsigned
less than 32 bits on your system), You can also changeproduct
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.
推荐阅读
- python - 如何在 aws quicksight 中安排或自动刷新数据集
- javascript - 当我将其插入另一个 div 时,自动将宽度调整为内部加载的随机图像数量的 ContainerDiv 会破裂
- javascript - 为什么 cookie 随每个请求一起发送,但在浏览器控制台或 document.cookie 中不可见?
- mysql - 如何在 Amazon LightSales 中更改字符集服务器的属性?
- javascript - 如何在grapesjs中为js代码添加选项?
- mysql - 如何使用spring boot通过邮递员操作数据
- firebase - Firebase 部署错误 - 尝试解析函数触发器时出现未知问题。请确保您使用的是 Node.js v6 或更高版本
- c++ - 为什么即使语句评估为假,我的循环也会在睡眠后连续循环
- mysql - MySQL 分析查询 - 提高性能
- kubernetes - Istio - Kiali - 上游连接错误或在标头之前断开/重置