uj5u.com熱心網友回復:
空格要用const char*表示," "是字串,你可以用string.data()方法將字串轉為const char*uj5u.com熱心網友回復:
有沒有包含#include <string.h>
uj5u.com熱心網友回復:
#include<string.h>
#include<stdio.h>
int main(void)
{
char input[16]="abc,d";
char*p;
/*strtok places a NULL terminator
infront of the token,if found*/
p=strtok(input,",");
if(p)
printf("%s\n",p);
/*Asecond call to strtok using a NULL
as the first parameter returns a pointer
to the character following the token*/
p=strtok(NULL,",");
if(p)
printf("%s\n",p);
return 0;
}
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char sentence[]="This is a sentence with 7 tokens";
cout << "The string to be tokenized is:\n" << sentence << "\n\nThe tokens are:\n\n";
char *tokenPtr=strtok(sentence," ");
while(tokenPtr!=NULL) {
cout<<tokenPtr<<endl;
tokenPtr=strtok(NULL," ");
}
//cout << "After strtok,sentence=" << tokenPtr<<endl;
return 0;
}
百度百科上面的兩個例子,跟你的代碼對比下,看看少了啥
uj5u.com熱心網友回復:
正解
uj5u.com熱心網友回復:
謝謝各位的幫忙轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/45949.html
標籤:C++ 語言
