C++ void function求助!

来源:百度知道 编辑:UC知道 时间:2024/09/24 03:45:08
Show tokens (separate word into individual tokens that are separated by one or more spaces)

比如输入 this is a joke,right!?
输出应该是 Token 1: this
Token 2: is
Token 3: a
Token 4: joke,
Token 5: right!?

求达人教小弟!! 感激不尽
要求用void function做,user输入string

//呵呵,刚写的,应该能满足你的要求了!不过,你提问不给悬赏,一般是没人回答
//你的提问的,尤其是要人动手编写代码!
#include <stdio.h>
#include <iostream>
#include <cstring>
using namespace std;

void spacestring(string & input);

int main(int argc, char **argv)
{
cout<<"Please enter a string : \n";
string str_input; //输入字符串的存放处
getline(cin,str_input);//取一整行的输入
spacestring(str_input);//调用函数,分解,显示字符串

return 0;
}

void spacestring(string & input)//分解 显示字符串 函数
{
string temp_string;
int len_string(0); //输入字符串长度
int npos_last(0); //起始位置
int npos_next(0); //终止位置
int count_word(0); //单词计数
int i(0);

len_string = input.size(); //赋值为输入字符串长度
if (0 == len_string) //空字符串直接返回
return;
while (i<len_string) { //进入循环,超出输入字符串长度,退出
npos_next = input.find(' ',npos_last);
if ((npos_next - npos_last) > 0) { //非空格,查找成功