如何使用的strtok() [英] How to use strtok()

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

问题描述

我正在写一个C程序来研究的strtok()的用法的用法。这里是我的code:

 #包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;主要()
{
    ABC的char [100] =LS&安培;;
    字符* TOK;    TOK = strtok的(美国广播公司,);
    而(托克!= NULL)
    {
            的printf(%S,TOK);
            TOK =的strtok(NULL,);
    }
    的printf(\\ n \\ n \\ n \\ n \\ n%S,TOK);
    返回0;
}

据打印的输出如下:

  LS&安培;
(空值)

但我想它来打印'和;'在第二个printf语句。我该怎么做?我需要这部分对我的家庭作业项目。谁能帮我?

感谢你在前进! :)


解决方案

  1. 确保你能识别的极限打印什么,当你打印。

  2. 在打印的消息的末尾输出换行;如果你这样做的信​​息是更可能出现及时。

  3. 请不要打印空指针作为字符串; 的printf不是所有版本()会表现很好 - 他们中的一些核心转储

code:

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;INT主要(无效)
{
    ABC的char [] =LS&安培;;
    字符* TOK;
    字符* PTR = ABC;    而((TOK =的strtok(PTR,))!= NULL)
    {
        的printf(<<%S>> \\ N,TOK);
        PTR = NULL;
    }
    返回0;
}

或(优化,自我礼貌):

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;INT主要(无效)
{
    ABC的char [] =LS&安培;;
    字符* TOK = ABC;    而((TOK = strtok的(TOK))!= NULL)
    {
        的printf(<<%S>> \\ N,TOK);
        托克= NULL;
    }
    返回0;
}

输出:

 << LS>>
<<&安培;>>

您可以选择自己的标记字符,但不能与XML或HTML搞乱,我找了双尖括号作业相当不错的。

您还可以使用循环结构在写第二个呼叫的成本的strtok()(这是最低的成本,但可能会被认为违反了DRY原则:不要重复自己):

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;INT主要(无效)
{
    ABC的char [] =LS&安培;;
    字符* TOK = strtok的(美国广播公司,);    而(托克!= NULL)
    {
        的printf(<<%S>> \\ N,TOK);
        TOK =的strtok(NULL,);
    }
    返回0;
}

相同的输出。


修订要求


  

我想补充的,而循环和打印<$ C $外的的printf()语句C>&安培; '之外。我需要它,因为我想在程序中另一个变量后进行比较。有没有什么办法这样做呢?


是的,通常有一个方式做几乎任何事情。这似乎工作。它也可以三立,如果有更多的标记来解析,或者如果仅仅是&安培; 解析,或者如果没有记号。显然,外循环体可以做成一个功能,如果你愿意的话;这将是明智的这样做,甚至

 的#include&LT;&stdio.h中GT;
#包括LT&;&string.h中GT;INT主要(无效)
{
    CHAR测试[] [16] =
    {
        ls -l命令-s&安培;,
        的ls&放大器;,
        与&amp;,
        ,
        
    };    用于(为size_t我= 0; I&LT;的sizeof(测试)/ sizeof的(测试[0]);我++)
    {
        的printf(起初:其中;&LT;%S&GT;&GT; \\ N,测试[I]);
        字符* tok1 = strtok的(测试[I],);
        字符* TOK;        而((TOK =的strtok(NULL,))!= NULL)
        {
            的printf(循环体:其中;&LT;%S&GT;&GT; \\ N,tok1);
            tok1 = TOK;
        }
        如果(tok1!= NULL)
            的printf(后循环:其中;&LT;%S&GT;&GT; \\ N,tok1);
    }    返回0;
}

输出:

 最初:&LT;&LT; ls -l命令-s和放大器;&GT;&GT;
循环体:其中;&LT; LS&GT;&GT;
循环体:其中;&LT;&-l GT;&GT;
循环体:其中;&LT;&-s GT;&GT;
交的环:其中;&所述;&放大器;&GT;&GT;
最初:其中;&所述; LS&放大器;&GT;&GT;
循环体:其中;&LT; LS&GT;&GT;
交的环:其中;&所述;&放大器;&GT;&GT;
最初:其中;&所述;&放大器;&GT;&GT;
交的环:其中;&所述;&放大器;&GT;&GT;
最初:其中;&下; &GT;&GT;
最初:其中;&所述;&GT;&GT;

请注意在最后两个例子标记为自己如何支付。你不能告诉那些没有分开的标记。

I'm writing a C program to study the usage of strtok() usage. Here is my code:

#include<stdio.h>
#include<string.h>

main()
{
    char abc[100] = "ls &";
    char * tok;

    tok = strtok(abc, " ");
    while(tok!=NULL)
    {
            printf("%s", tok);
            tok = strtok(NULL, " ");
    }
    printf("\n\n\n\n\n%s", tok);
    return 0;
}

It is printing the following output:

ls&




(null)

But I want it to print '&' at the second printf statement. How do I do it? I need this part for my homework project. Can anyone help me out?

Thank you in advance! :)

解决方案

  1. Make sure you can identify the limits of what you print when you're printing.
  2. Output newlines at the end of printed messages; the information is more likely to appear in a timely manner if you do that.
  3. Don't print NULL pointers as strings; not all versions of printf() will behave nicely — some of them dump core.

Code:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char abc[] = "ls &";
    char *tok;
    char *ptr = abc;

    while ((tok = strtok(ptr, " ")) != NULL)
    {
        printf("<<%s>>\n", tok);
        ptr = NULL;
    }
    return 0;
}

Or (optimized, courtesy of self.):

#include <stdio.h>
#include <string.h>

int main(void)
{
    char abc[] = "ls &";
    char *tok = abc;

    while ((tok = strtok(tok, " ")) != NULL)
    {
        printf("<<%s>>\n", tok);
        tok = NULL;
    }
    return 0;
}

Output:

<<ls>>
<<&>>

You can choose your own marker characters, but when not messing with XML or HTML, I find the double angle brackets reasonably good for the job.

You can also use your loop structure at the cost of writing a second call to strtok() (which is a minimal cost, but might be argued to violate the DRY principle: Don't Repeat Yourself):

#include <stdio.h>
#include <string.h>

int main(void)
{
    char abc[] = "ls &";
    char *tok = strtok(abc, " ");

    while (tok != NULL)
    {
        printf("<<%s>>\n", tok);
        tok = strtok(NULL, " ");
    }
    return 0;
}

Same output.


Revised requirement

I would like to add a printf() statement outside the while loop and print '&' outside. I need it since I want to compare it later with another variable in the program. Is there any way to do so?

Yes, there is usually a way to do almost anything. This seems to work. It also works sanely if there are more tokens to parse, or if there's only the & to parse, or if there are no tokens. Clearly, the body of the outer loop could be made into a function if you so wished; it would be sensible to do so, even.

#include <stdio.h>
#include <string.h>

int main(void)
{
    char tests[][16] =
    {
        "ls -l -s &",
        "ls &",
        "&",
        "    ",
        ""
    };

    for (size_t i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
    {
        printf("Initially: <<%s>>\n", tests[i]);
        char *tok1 = strtok(tests[i], " ");
        char *tok;

        while ((tok = strtok(NULL, " ")) != NULL)
        {
            printf("Loop body: <<%s>>\n", tok1);
            tok1 = tok;
        }
        if (tok1 != NULL)
            printf("Post loop: <<%s>>\n", tok1);
    }

    return 0;
}

Output:

Initially: <<ls -l -s &>>
Loop body: <<ls>>
Loop body: <<-l>>
Loop body: <<-s>>
Post loop: <<&>>
Initially: <<ls &>>
Loop body: <<ls>>
Post loop: <<&>>
Initially: <<&>>
Post loop: <<&>>
Initially: <<    >>
Initially: <<>>

Note how the markers pay for themselves in the last two examples. You couldn't tell those apart without the markers.

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

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