从句子中拆出单词

利用指针,和strtok函数

字符数组实际上是char*即char类型的指针

char * strtok(char * str, const char * delim);
连续调用该函数若干次,可以做到:从str中逐个抽取出被字符串delim中的字符分隔
开的若干个子串。

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
char str[50];
int main()
{
    char *p;
    gets(str);
    p=strtok(str," ");
    while(p!=NULL)
    {
        cout<<p<<endl;
        /*char *a=p;
        cout<<a<<endl;*/
        p=strtok(NULL," ");//注意两个strtok的第一个参数,第一个是字符串名,第二个是NULL(空指针)
    }
}
原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/6219046.html