使用Pointers和strtok() [英] Using Pointers and strtok()

查看:85
本文介绍了使用Pointers和strtok()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个链表,因为我是C的新手,所以需要您的帮助. 我需要输入一个看起来像这样的字符串:(word)_#_(year)_#_(DEFINITION(UPPER CASE))

I'm building a linked list and need your assistance please as I'm new to C. I need to input a string that looks like this: (word)_#_(year)_#_(DEFINITION(UPPER CASE))

Ex:输入一个字符串 输入:invest_#_1945_#_TRADE

Ex: Enter a string Input: invest_#_1945_#_TRADE

基本上,我正在寻找一个可以扫描DEFINITION的函数,并且将与它有关的词带给我. 输入要在词典中搜索的单词 输入:贸易 输出:在投资"一词中找到"TREADE"

Basically I'm looking to build a function that scans the DEFINITION and give's me back the word it relates to. Enter a word to search in the dictionary Input: TRADE Output: Found "TREADE" in the word "invest"

到目前为止,我设法使用strtok()函数,但是现在我不确定该如何打印第一个单词.

So far I managed to come up using the strtok() function but right now I'm not sure what to do about printing the first word then.

这就是我能想到的:

  char split(char words[99],char *p)
 {

p=strtok(words, "_#_");
while (p!=NULL)
{
    printf("%s\n",p);
    p = strtok(NULL, "_#_");
}
return 0;

 }


 int main()
 {
char hello[99];
char *s = NULL;

printf("Enter a string you want to split\n");
scanf("%s", hello);
split(hello,s);
return 0;
 }

关于我该怎么办的任何想法?

Any ideas on what should I do?

推荐答案

我认为您的问题是如何从格式化的字符串中提取三位信息.

I reckon that your problem is how to extract the three bits of information from your formatted string.

函数strtok不能像您认为的那样起作用:第二个参数不是文字分隔字符串,而是用作分隔符的一组字符的字符串.

The function strtok does not work as you think it does: The second argument is not a literal delimiting string, but a string that serves as a set of characters that are delimiters.

对于您来说,sscanf似乎是更好的选择:

In your case, sscanf seems to be the better choice:

#include <stdlib.h>
#include <stdio.h>

int main()
{
    const char *line = "invest_#_1945 _#_TRADE  ";

    char word[40];
    int year;
    char def[40];
    int n;

    n = sscanf(line, "%40[^_]_#_%d_#_%40s", word, &year, def);

    if (n == 3) {
        printf("word:   %s\n", word);
        printf("year:   %d\n", year);
        printf("def'n:  %s\n", def);
    } else {
        printf("Unrecognized line.\n");
    }

    return 0;
}

函数sscanf根据给定的模式检查给定的字符串.大致而言,该模式由以百分号开头的格式说明符,表示任意数量的空格字符(包括无)的空格以及必须与varbatim匹配的其他字符组成.格式说明符产生必须存储的结果.因此,对于每个说明符,必须在格式字符串后给出结果变量.

The function sscanf examines a given string according to a given pattern. Roughly, that pattern consists of format specifiers that begin with a percent sign, of spaces which denote any amount of white-space characters (including none) and of other characters that have to be matched varbatim. The format specifiers yield a result, which has to be stored. Therefore, for each specifier, a result variable must be given after the format string.

在这种情况下,有几个块:

In this case, there are several chunks:

%40[^_]最多将40个不是下划线的字符读入char数组.这是读取字符串的特殊情况. sscanf中的字符串实际上是单词,不能包含空格.但是,下划线将是字符串的一部分,因此,为了不占用第一个定界符的下划线,您必须使用符号[^(chars)],这意味着:不包含给定字符的任何字符序列. (插入符号在此处进行求反,[(chars)]表示给定字符的任何序列.)

%40[^_] reads up to 40 characters that are not the underscore into a char array. This is a special case of reading a string. Strings in sscanf are really words and may not contain white space. The underscore, however, would be part of a string, so in order not to eat up the underscore of the first delimiter, you have to use the notation [^(chars)], which means: Any sequence of chars that do not contain the given chars. (The caret does the negation here, [(chars)] would mean any sequence of the given chars.)

_#_从字面上匹配第一个定界符,即,仅当下一个字符为下划线哈希标记下划线时.

_#_ matches the first delimiter literally, i.e. only if the next chars are underscore hash mark, underscore.

%d将十进制数读取为整数.请注意,此处必须使用&给出整数的地址.

%d reads a decimal number into an integer. Note that the adress of the integer has to be given here with &.

_#_与第二个分隔符匹配.

_#_ matches the second delimiter.

%40s将最多40个非空白字符的字符串读取到char数组中.

%40s reads a string of up to 40 non-whitespace characters into a char array.

该函数返回匹配结果的数量,如果该行有效,则应为3.函数sscanf可能很麻烦,但是对于快速而肮脏的输入来说,这也许是最好的选择.

The function returns the number of matched results, which should be three if the line is valid. The function sscanf can be cumbersome, but is probably your best bet here for quick and dirty input.

这篇关于使用Pointers和strtok()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆