如何解决C - 拆分字符串
如果我想把它变成 4 个字,我该如何处理这个字符串。如果我使用 sscanf,它会将“面罩”这个词一分为二。或者可以以某种方式使用 sscanf 来防止这种情况发生?
输入:
2021-01-01 2021-7-1 'Face masks' "Wear everywhere"
2000-08-05 2010-8-8 LOCKDOWN 'xxxxx'
输出:
2021-01-01
2021-7-1
'Face masks'
"Wear everywhere"
....
解决方法
scanf()
对于您的目的来说不够强大,因为可选引号和可能的空词(''
或 ""
)。
这是一个通用的手工编码解析器:
#include <stdio.h>
#include <string.h>
// split a string into up to count words stored in a 2D array of char
int split(char output[][100],int count,const char *str) {
// recognises space separated words and quoted content without
// embedded quotes of the same type. Quotes are stripped in output.
for (int i = 0; i < count; i++) {
const char *start;
int len;
str += strspn(str," \t\f\v\r\n");
if (*str == '\0')
return i;
if (*str == '\'') {
start = ++str;
len = strcspn(str,"\'");
str += len;
str += (*str == '\'');
} else
if (*str == '\"') {
start = ++str;
len = strcspn(str,"\"");
str += len;
str += (*str == '\"');
} else {
start = str;
len = strcspn(str," \t\f\v\r\n");
str += len;
}
snprintf(output[i],sizeof(output[i]),"%.*s",len,start);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。