我试图在strcasestr中访问char * ,但它似乎没有任何效果 下面是代码:
#define NUMOFADDTYPES 5
const char* milkAdditions[] = {"Cream", "Half-and-half", "Whole-Milk",
"Part-skim", "skim", "non-dairy", NULL};
const char* syrupAdditions[] = {"Vanilla", "Almond", "Raspberry", NULL};
const char* sweetenerAdditions[] = {"white-sugar", "sweetener",
"Raw-cane", "honey", NULL};
const char* spiceAdditions[] = {"Cinnamon", "cardamon", NULL};
const char* alcoholAdditions[] = {"Brandy", "Rum", "Whiskey",
"Aquavit", "Kahlua", NULL};
const char** additions[] = {milkAdditions, syrupAdditions,
sweetenerAdditions, spiceAdditions,
alcoholAdditions};
char *ptr;
int i, j;
printf("Dump of additions[j][i]\n");
for(j = 0; j < NUMOFADDTYPES; j++)
{
for(i = 0; (additions[j])[i] != NULL; i++)
{
printf("%d %d\t%s\n", j, i, additions[j][i]);
if((ptr = strcasestr((additions[j])[i], string)) != NULL)
{
ptr += strlen((additions[j])[i]);
getVolume(ptr);
}
}
输出是:
Dump of additions[j][i]
0 0 Cream
0 1 Half-and-half
0 2 Whole-Milk
0 3 Part-skim
0 4 skim
0 5 non-dairy
1 0 Vanilla
1 1 Almond
1 2 Raspberry
2 0 white-sugar
2 1 sweetener
2 2 Raw-cane
2 3 honey
3 0 Cinnamon
3 1 cardamon
4 0 Brandy
4 1 Rum
4 2 Whiskey
4 3 Aquavit
4 4 Kahlua
任何帮助将不胜感激
编辑:
抱歉没有添加
char string[] = "AcceptAdditions: Cream;lots white-sugar;dash\r\n\r\n";
getAddition(string);
解决
答案 0 :(得分:0)
char *strcasestr(const char *haystack, const char *needle);
strcasestr()函数在字符串haystack中找到第一次出现的子串针。
所以,不是必须
strcasestr(string, (additions[j])[i])
而不是
strcasestr((additions[j])[i], string)
答案 1 :(得分:0)
很抱歉,我误解了手册页,正在用主字符串
搜索子字符串char *strcasestr(const char *haystack, const char *needle);
谢谢你的回答
答案 2 :(得分:0)
编辑后:
你正在比较每个“奶油”,“一半和一半”,......“Kahlua”到“AcceptAdditions:Cream;很多白糖;破折号\ r \ n \ r \ n”。
strcasestr
永远找不到匹配!
我认为你想要在较小的部分打破你的字符串并检查小部分。
char string[] = "AcceptAdditions: Cream;lots white-sugar;dash\r\n\r\n";
const char *accepted[] = {"cream", "white-sugar", "dash"};
然后针对每个接受的元素检查每个项目
for (int k = 0; k < sizeof accepted / sizeof *accepted; k++) {
if ((ptr = strcasestr((additions[j])[i], accepted[k])) != NULL)
{
//ptr += strlen((additions[j])[i]);
getVolume(ptr);
}
}