在字符串中查找问题的c++脚本

c++ Script that finds questions in string

本文关键字:c++ 脚本 问题 查找 字符串      更新时间:2023-10-16

我是编码新手,已经编码了大约一个星期,我正在尝试做一个脚本,找到"?"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]);
  }
}

我想我以前见过这个问题