【问题标题】:C++ strings, punctuation, arraysC++ 字符串、标点符号、数组
【发布时间】:2014-05-13 00:40:23
【问题描述】:

我需要能够仅使用 c++ 字符串才能更改下面的输入段落。我遇到的问题是,当我在末尾拉出带有标点符号的东西时,例如“programs-”,它会将它作为“programs-”而不是“programs”和“-”分别拉入我的数组中。我需要知道如何执行此操作,以便可以将所有“-”替换为“.”。

有人如何将两者分开并将每个放在数组中自己的块中?

#include <iostream>
#include <fstream>

using namespace std;


void read(string line[], string para[], int &d)
{
string temp;
int i = 0;

ifstream myfile ("paragraphwordsub.txt");
if (myfile.is_open())
{
    while ( temp != "$" )
    {
        myfile >> temp;
        line [i] = temp;
        cout << line[i] << " ";
        i++;
    }
    cout << endl;
    i=0;
    while (!myfile.eof())
    {
        myfile >> temp;
        para[i] = temp;
        cout << para[i] << " ";
        d++;
        i++;
    }

 myfile.close();
}

else cout << "Unable to open file";

cout << endl << endl << i << endl << para[73] << endl;

return;

}

int main()
{
  int const SIZE = 100;
  string a [SIZE];
  string b [SIZE];
  int counter = 0;

  read(a, b, counter);

  cout << endl << endl << counter << endl;


  return 0;

}

输入:

谁的节目是这样的? 有一些重要的计划要完成,而汤米被要求这样做——汤米确信山姆会这样做——萨姆森本可以这样做 是这样的,但尼科尔森德斯做了这样的事——山姆对此很生气,因为 这就是汤米的节目——汤米认为参孙可以这样做,但 Nicholsonnders 意识到汤米不会那样做——结果是 当尼科尔森德斯做了萨姆本可以做的事时,汤米责怪山姆-

【问题讨论】:

  • 我喜欢示例文本毫无意义。
  • 你可以使用isalpha函数来检查一个字符是否是字母。
  • 示例文本应该没有意义,因为我将用不同的文本替换其中的文本。这包括将所有“-”更改为“。”尼尔,我不确定如何使用字符串并将其从文件中提取出来。

标签: c++ arrays string


【解决方案1】:

我不完全明白你想用它做什么,但这里有一个例子,说明如何在保持标点符号和空格分开的同时对段落进行标记。我使用了向量而不是数组,但它应该很容易切换:

#include <vector>
#include <fstream>
#include <ctype.h>
#include <iostream>
#include <string>

class Words {
  std::vector<std::string> words_;
  bool lastCharacterIsAlphaNumeric_;

  void addAlphaNumeric(char character);
  void startNewWord(char character) {  words_.emplace_back(1, character); }
  void addNewWordForNonAlphaNumeric(char character);
  void clear() { words_.clear(); lastCharacterIsAlphaNumeric_ = false; }

public:
  Words() : lastCharacterIsAlphaNumeric_(false) {}
  void read(std::string filename);
  void print() { for(auto& word : words_) std::cout << word << std::endl; }
};

void Words::addAlphaNumeric(char character) {
  if (!lastCharacterIsAlphaNumeric_) {
    startNewWord(character);
    lastCharacterIsAlphaNumeric_ = true;
  } else {
    words_.back().push_back(character);
  }
}

void Words::addNewWordForNonAlphaNumeric(char character) {
  startNewWord(character);
  lastCharacterIsAlphaNumeric_ = false;
}

void Words::read(std::string filename) {
  clear();
  std::ifstream myfile (filename);
  char tmp;

  while(myfile.good()) {
    myfile.get(tmp);
    if(isalnum(tmp)) {
      addAlphaNumeric(tmp);
    } else {
      addNewWordForNonAlphaNumeric(tmp);
    }
  }
}

int main()
{
  Words words;
  words.read("paragraphwordsub.txt");
  words.print();
}

【讨论】:

    【解决方案2】:

    如果我对问题的理解正确,您希望在输入中找到每个出现的子字符串“-”并将其替换为子字符串“.”。

    您可能一直希望,因为 operator>> 从输入中一次读取一个“单词”,它会在“programs”的最后一个字母之后停止;但实际上它仅在涉及空格或输入结束时才停止。似乎有一个流操纵器可以用来告诉它在标点符号处停止,但我不知道有任何这样的操纵器。

    你可以试试这样的:

    myfile >> temp;
    size_t position = temp.find("-");
    while (found!=std::string::npos) {
      temp.replace(position, 1, ".");
    }
    

    如果您使用字符串变量而不是文字“-”和“.”,您可以将其概括为进行任何您想要的替换(但是在替换()的参数中代替常量 1,您必须使用要替换的子字符串的长度)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 2015-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多