-6

这是我的代码:

char *reason = strtok(NULL, "\n");
std::string kickreason = "No reason";
if(reason)
kickreason = reason;

如何让“kickreason”只读 1 个字?所以如果用户输入“haha lol XD”它只会读到“haha”?

4

3 回答 3

2

将字符串放入 astd::istringstream并使用普通输入运算符>>

std::istringstream iss("haha lol XD");
std::string word;

iss >> word;

std::cout << "First word is \"" << word << "\"\n";
于 2013-11-06T09:34:21.340 回答
0

stringstream::operator>>(string)? 那是您需要查看的地方。

像这样:

istringstream str("no reason");
string oneword = "";
str >> oneword;
于 2013-11-06T09:33:43.923 回答
0

如何使用 strtok()

#include <stdio.h>
#include <iostream>

int main(int argc, char *argv[]) {
    char sentence[] = "haha lol XD";  
    char * word;
    std::cout << "Your sentence: " << sentence << std::endl;
    word = strtok (sentence," ");
    std::cout << "First word: " << word << std::endl;

    return 0;
}

输出是

Your sentence: haha lol XD
First word: haha
于 2013-11-06T09:37:48.517 回答