在字符串中查找问题的c++脚本
c++ Script that finds questions in string
我是编码新手,已经编码了大约一个星期,我正在尝试做一个脚本,找到"?"answers"。"在脚本中,然后输出它们在脚本中的位置,我使用这些值将问题打印到文本文件。
但它实际上不起作用
如果你像这样输入值,它可以工作。
myfile << test.substr( 18, 20 )
但是像这样它不工作,它只是打印整个脚本从点[0]的值直到脚本结束。
myfile << test.substr( dot[0], interrogation[0] )
我用来在字符串中找到"?"位置的方法也不是很准确。
哪里有。
if(x > 0){
我有一个while循环,但出于调试原因我替换了它。这是整个代码。如果你能帮助我,我会很感激。谢谢。
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main (){
std::vector< int > interrogation ;
std::vector< int > dot;
string look = "?";
string look_again = ".";
string test = "ver. o que e isto? nao sei. ola? adeus. fghfghfhfghf";
string::size_type pos = test.find(look);
string::size_type sop = test.find(look_again);
string::size_type exc = test.find(look_again_again);
while (pos != std::string::npos)
{
int a = pos ;
int b = sop;
cout << " . found at : " << sop << std::endl;
cout << " ? found at : " << pos << std::endl;
interrogation.push_back(a);
dot.push_back(b);
string fragment = test.substr (0 , pos ); // works
//cout << fragment << endl ;
string fragment2 = test.substr (0 , sop ); // works
//cout << fragment2 << endl ;
pos = test.find(look, pos + 1);
sop = test.find(look_again, sop + 1);
}
int x = 1;
if(x > 0){
int a = 1;
int q = dot[a];
int w = interrogation[a];
// to save text
// to save text
string save = "saved_question.txt" ;
ofstream myfile;
myfile.open (save.c_str(), ios::app);
myfile << test.substr( 18, 20 ) + "n" ;
myfile.close();
cout << "Question saved in text file" << endl;
}
}
代码还没有完成,但我得到了帮助。由于
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main (){
std::vector< int > interrogation ;
std::vector< int > dot;
//std::vector< int > exclamation;
string look = "?";
string look_again = ".";
string look_again_again = "!";
string test = " ver.o que e isto? nao sei. ola? adeus.";
string::size_type pos = test.find(look);
string::size_type sop = test.find(look_again);
while (pos != std::string::npos)
{
int a = pos ;
int b = sop;
cout << " . found at : " << sop << std::endl;
cout << " ? found at : " << pos << std::endl;
// cout << " ! found at : " << exc << std::endl;
interrogation.push_back(a);
dot.push_back(b);
//exclamation.push_back(c);
string fragment = test.substr (0 , pos ); // works
//cout << fragment << endl ;
string fragment2 = test.substr (0 , sop) ; // works
//cout << fragment2 << endl ;
string fragment3 = test.substr (dot.back() + 1, interrogation.back() - dot.back()); // works
cout << fragment3 << endl ;
pos = test.find(look, pos + 1);
sop = test.find(look_again, sop + 1);
}
}
你可以这样做:
void function(string str) {
string sentence = "";
for(int i=0; i < str.length(); i++) {
if(str[i] == '.' || str[i] == '!')
sentence = ""; // the sentence is not a question so clear the sentence
else if(str[i] == '?') {
sentence.push_back(str[i]);
cout << sentence << endl; // output the question - just replace cout with your ofstream
}
else
sentence.push_back(str[i]);
}
}
我想我以前见过这个问题
相关文章:
- 如何通过cpp程序运行shell脚本
- 在程序中编写脚本来编写和编译代码
- 如何在c++中使用system()来运行包含空格的python脚本
- 在cygwin中运行c++脚本时发生错误
- 如何将真正的字符串从python c-api转换为python脚本
- 用于将C++代码转换为 Web 程序集的脚本未终止
- 如何使用C++初始化向量;脚本化值不是数组、指针或矢量错误
- 在原子中使用犰狳与脚本
- 我适合在我的构建系统中使用 shell 脚本而不是AX_CHECK_COMPILE_FLAG吗?
- CANoe - 无法在 CAPL 脚本中打开.dll
- 在从Qt调用的Python脚本中导入OpenCV崩溃
- 将数组/向量发送到 c++ 脚本的 cython 示例
- 脚本的多重定义::LState
- 从C++应用获取PowerShell脚本输出
- 用户控制从 c++ 到 java 脚本的 Webassembly 访问调用
- 使用 Main() 返回值作为 python 脚本的参数
- 如何从测试脚本中运行WAF编译C++程序?
- bash脚本在使用popen()时不返回代码以调用C ++程序
- 如何注册枚举类型到QJSEngine,以便在脚本环境中使用?
- FindPOCO CMAKE 脚本无法找到 MYSQL::client