[英]How to split or tokenize an array of strings by a space " " delimiter to find the second word in each string
我有一个包含一堆字符串的数组。 我想在这个数组中包含的每个字符串中找到第二个单词,这样我就可以按句子中的第二个单词的字母顺序对句子进行排序。 我尝试使用strtok
但它不适用于标记字符串数组。 分隔符只是每个单词之间的空格。 我必须在 C 中做到这一点。
C 中的排序通常使用qsort
完成。 接口是通用的,只是比较功能是特定于排序要求的。
假设数组的元素是 C 字符串,那么这种情况下的比较函数可能如下所示:
#include <string.h>
int cmp(const void * pv1, const void * pv2)
{
const char * const * pp1 = pv1;
const char * const * pp2 = pv2;
const char * p1 = *pp1 ?*pp1 :""; /* Let's do it the ORxCLx way and tolerate NULLs, by treating them as empty-string. */
const char * p2 = *pp2 ?*pp2 :""; /* Same. */
/* Try to get space between 1st and 2nd word: */
const char * pb1 = strchr(p1, ' ');
const char * pb2 = strchr(p2, ' ');
/* In case there was no 1st space
make begin point to right after the 1st word's end: */
pb1 = pb1 ?pb1+1 :p1 + strlen(p1);
pb2 = pb2 ?pb2+1 :p2 + strlen(p2);
/* Try to get space between 2nd and 3rd word: */
const char * pe1 = strchr(pb1, ' ');
const char * pe2 = strchr(pb2, ' ');
/* In case there was no 2nd space
make end point right after the 2nd's word's end: */
pe1 = pe1 ?pe1 :pb1 + strlen(pb1);
pe2 = pe2 ?pe2 :pb2 + strlen(pb2);
/* Calculate 2nd word's length: */
size_t s1 = (size_t)(pe1 - pb1);
size_t s2 = (size_t)(pe2 - pb2);
return strncmp(pb1, pb2, s1 > s2 ?s2 :s1);
}
像这样使用它:
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *, const void *);
int main(void)
{
const char * as[] = {
"e 5 y",
"b 2",
"c 3 z",
"d 4 x",
"c 3 ",
"a 1",
"c 3"
};
qsort(as, sizeof as / sizeof *as, sizeof *as, cmp);
for (size_t i = 0; i < sizeof as / sizeof *as; ++i)
{
printf("'%s'\n", as[i]);
}
}
并得到:
'a 1'
'b 2'
'c 3 z'
'c 3'
'c 3 '
'd 4 x'
'e 5 y'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.