字符串的截取问题,急急急!!各位大侠帮帮忙呀

luchangxing09 2011-07-06 03:57:26
例如:我输入任意字符串s[]="begin hkhkhk 7997 ljlj 4567r end"
设计一个程序begin作为程序的开始,end作为程序的结束
最后我要的结果是 hkhkhk 7997 ljlj 4567r
...全文
131 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
你妹的特盗不 2011-07-07
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 luchangxing09 的回复:]
如果这个字符串很大怎么办呀,可能有几千个?如上述的方法就行不通
[/Quote]

怎么行不通,随便多大都行得通..
"begin " "end" 只有这么长,再长的字符串,都能得到长度
luchangxing09 2011-07-06
  • 打赏
  • 举报
回复
如果这个字符串很大怎么办呀,可能有几千个?如上述的方法就行不通
你妹的特盗不 2011-07-06
  • 打赏
  • 举报
回复

WCHAR buf[]=L"begin ss tt gg bbada rwas a aea d eqwrcx end";
TRACE(L"%.*s\n",_countof(buf)-sizeof("begin ")-sizeof("end"),(buf+sizeof("begin ")));//mfc调试的,直接把trace改成print就可以了
至善者善之敌 2011-07-06
  • 打赏
  • 举报
回复

#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <ITERATOR>

#define INPUT_STRING "begin hkhkhk 7997 ljlj 4567r end"

typedef std::vector<std::string> strvec;

int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;

istringstream iss(INPUT_STRING);
strvec strList;
string strTemp;

while(iss >> strTemp)
{
strList.push_back(strTemp);
}

//vector<std::string>::iterator iter=strList.begin();
strList.erase(std::find(strList.begin(), strList.end(), "begin"));
strList.erase(std::find(strList.begin(), strList.end(), "end"));
// out put the result
copy(strList.begin(), strList.end(), ostream_iterator<string>(cout, "\n"));

return 0;
}
mibbadman 2011-07-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 dizuo 的回复:]
C++下为什么不用istringstream

C/C++ code
#include <sstream>
char s[]="begin hkhkhk 7997 ljlj 4567r end";
istringstream istr( string(s) );
string section;
while( istr >> section )
{
cout << sectio……
[/Quote]++
ningto.com 2011-07-06
  • 打赏
  • 举报
回复
q结束输入

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

vector<string> vs;
string str;

while (cin >> str)
{
if (str == "q")
{
break;
}
vs.push_back(str);
}

for (vector<string>::iterator iter = vs.begin() + 1; iter != vs.end() - 1; ++iter)
{
cout << *iter << " ";
}

system("pause");
return 0;
}
ryfdizuo 2011-07-06
  • 打赏
  • 举报
回复
C++下为什么不用istringstream
#include <sstream>
char s[]="begin hkhkhk 7997 ljlj 4567r end";
istringstream istr( string(s) );
string section;
while( istr >> section )
{
cout << section;
}
tianma2005123 2011-07-06
  • 打赏
  • 举报
回复
正则表达式,字符串匹配。
c_losed 2011-07-06
  • 打赏
  • 举报
回复
begin和end 自己过滤下就ok了
c_losed 2011-07-06
  • 打赏
  • 举报
回复

/* STRTOK.C: In this program, a loop uses strtok
* to print all the tokens (separated by commas
* or blanks) in the string named "string".
*/

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

char string[] = "begin hkhkhk 7997 ljlj 4567r end";
char seps[] = " ";
char *token;

void main( void )
{
printf( "%s\n\nTokens:\n", string );
/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );
/* Get next token: */
token = strtok( NULL, seps );
}
}

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧